using transform.rotate to mirror a character - unity3d

I am trying to figure out how to use transform.rotation to simply rotate a character 180 degrees, as if the character is sitting in front of a mirror.
Initially, I used the following to set the character model to the game object it is attached to, which works fine:
target = gameObject.transform;
origPos = target;
target.transform.rotation = transform.rotation;
Now, I would like to turn it around and mirror its movements. How do I do this?
As a first silly attempt, I tried putting a minus sign before rotation, thinking that it would negate all 4 values of w, x, y, z but of course that was just stupid, as I could not use such an operand here.
What is the simplest way of doing this?

transform.scale should be able to sort this, just multiply by -1 on the axis you require so
transform.scale = new Vector3(1.0f, -1.0f, 1.0f);
would mirror in the y axis
http://docs.unity3d.com/ScriptReference/Transform-localScale.html

to rotate something using euler angle values use Quaternion.Euler
target.transform.rotation = Quaternion.Euler(x, y, z);

target.transform.rotation = Quaternion.AngleAxis(180, transform.up);

Related

Rotating rotation value by different normalized vector directions

I have written a script in Unity which takes a SkinnedMeshRenderer and AnimationClip and rotates the vertices in each by a specified number of degrees. It looks mostly correct except that rotations seem to be incorrect. Here is an example bone rotation (in euler angles) in the skeleton along with the correct values that would be needed for the animation to look correct.
With no rotation: (0, 0, -10)
Rotated 90 degrees: (-10, 0, 0)
Rotate 180 degrees: (0, 0, 10)
I have been trying to find a way to rotate these bones to make this conversion make sense with the data I have here, but have come up short. I know I want to rotate these values around the Y axis, but don't actually want the Y value in the euler angle to change. I am aware I could just reorient the root bone around the Y axis and the problem would be solved, but I want to have no rotation in the Y axis. I am "fixing" some older animations that have unnecessary rotation values in them.
var localBoneRotation = new Quaternion(keysX[j].value, keysY[j].value, keysZ[j].value, keysW[j].value).eulerAngles;
var reorientedForward = Quaternion.AngleAxis(rotation, Vector3.up) * Vector3.forward;
localBoneRotation.x *= reorientedForward.x;
localBoneRotation.y *= reorientedForward.y;
localBoneRotation.z *= reorientedForward.z;
var finalRotation = Quaternion.Euler(localBoneRotation);
keysX[j].value = finalRotation.x;
keysY[j].value = finalRotation.y;
keysZ[j].value = finalRotation.z;
keysW[j].value = finalRotation.w;
I have also tried using a matrix and Vector3 but most of the time I end up with values in the Y. Perhaps I am going about this incorrectly. I just need to be able to specify an angle rotation and then have the input data match the final euler angles with each of these data points.

Only rotate one axis on Unity gameobject

I have inherited some code that simply does this....
transform.rotation = m.rotation;
I know that my model will always be standing upright and therefore it only needs to rotate around on one axis. How can I only change the Y rotation.
I can't seem to get this to work because I get stuck right away when I try:
transform.rotation = Quaternion.Euler(m.rotation.x, m.rotation.y, m.rotation.z);
Which does not align it the same as:
transform.rotation = m.rotation;
I was hoping to then just change the m.rotation.y. Any reason this doesn't work?
The m value looks like this
public MarkerInfo(string name, bool isVisible, Vector3 position, Quaternion rotation, Vector3 scale){
this.name = name;
this.isVisible = isVisible;
this.position = position;
this.rotation = rotation;
this.scale = scale;
}
If m stands for a c# class and not a uniy one, changing its rotations will have no effect. You need to apply your rotation to a unity Transform, that normally is an instance of a component of a gameobject of the scene. That way you can see in the scene the changes you apply to the transform of that specific gameobject.
I think you need to know what unity gameobject does MarkerInfo manipulate or stands for, get its Transform with gameObject.transform and rotate that.
With transform.RotateAround or manipulating directly yout transform's Transform.eulerAngles you should be able to get your rotation.
Take into account that this line: transform.rotation = Quaternion.Euler(m.rotation.x, m.rotation.y, m.rotation.z); you are trying wont do what I think you think it does. The x,y,z of a quaternion are not the euler angles.
To apply that rotation to the transform you need to do transform.rotation = m.rotation, so the fact those line do not do same make perfect sense.
The best ways to do this are by using Quaternion.Euler, which converts a Vector3 to a Quaternion (Unity's confusing variable for storing rotation), or by using Quaternion.Set, which sets the values of a Quaternion similar to Vector3.Set. The first one has 2 declarations: 1 where you pass a vector3 variable, and another where you pass 3 ints/floats. The following 2 lines of code both do the same thing using both declarations.
transform.rotation += Quaternion.Euler(Vector3.up);
transform.rotation += Quaternion.Euler(0, 1, 0);
The second method works like this (I don't know what the fourth one does, but it's typically 1):
// transform.Set(x, y, z, 1);
transform.Set(0, 45, 0, 1);
transform.Set(transform.x + 1, transform.y, transform.z, 1);
Quaternions are confusing and annoying at first, but Unity comes with a lot of fancy rotation-focused functions that coultn't be possible without them. You can use Quaternion.Euler to convert a Vector3 to a Quaternion and then use those functions on them. They could come in handy later. You can find them here under "public/static methods".
I suppose you want player to have same forward vector as other object but keep the original up vector?
I would avoid Euler Angles as much as possible because they are "glitchy" and try like that:
var forward = m.forward;
forward.y = 0;
transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
just resolve the case
m.forwad == Vector3.down || m.forward == Vector3.up
and it should be working

Rotate a quaternion / Change the axis it rotates around

Conceptually a quaternion can store the rotation around an axis by some degree.
Now, what is the numerically most robust and least calculation intensive way to rotate the axis?
For example when i have a quaternion, which rotates 90 degrees around the y-axis and i want those 90 degrees to be applied to some other arbitrary axis, described by a normalized vector.
EDIT: Since this also came up i added an answer on how to correctly change the axis a quaternion rotates around with another quaternion.
It is a bit unclear what your actual goal is by doing what you describe.
In order to actually keep the angle but change the axis you would use Quaternion.ToAngleAxis, alter the axis and then pass it back into Quaternion.AngleAxis
like e.g.
Quaternion someRotation;
someRotation.ToAngleAxis(out var angle, out var axis);
var newAxis = Vector3.up;
var newRotation = Quaternion.AngleAxis(angle, new axis);
Or you rotate an existing Quaternion by another one using * like
Quaternion newRotation = someRotation * Quaternion.Euler(90, 0, 0);
which would take the existing rotation and rotate it by 90° around the X axis.
#derHugo's solution, solves the problem i initially asked, but the seconds part of his answer isn't doing what he seemed to be describing. To rotate a quaternions axis of rotation with another quaternion you would need to apply the rotations differently.
E.g. you have a quaternion yQuaternion, which rotates 90° around the y-axis and want to rotate, it's rotation axis by 90° around the x-axis (which would result in a quaternion rotating 90° around the z-axis) you'd have to do the following.
// The quaternion, we want to "rotate"
var yQuaternion = Quaternion.Euler(0f, 90f, 0f);
// The quaternion we want to rotate by.
var xQuaternion = Quaternion.Euler(90f, 0f, 0f);
var result = xQuaternion * yRotation * Quaternion.Inverse(xQuaternion);
What happens here is that we first rotate backwards to our desired axis, then apply the rotation we want to use and then revert the rotation we initally applied.
NOTE: I'm quit sure, that saying "rotate a quaternion" isn't a valid term when talking about this quaternion operations.

Unity : To rotate 3d Object to align 2D isometric field

I want to rotate a car to align isometric field like above picture.
but i have any idea about that.
Do you have a some of idea to solve it?
Most quasi-isometric games use this:
x = 30, y = 45 degrees rotation
Of course, you might need to flip the values between axis or make one negative to align with your world.
Generally, I would recommend to choose either approach: Do it in 3d and position the orthographic camera so that it looks isometric/dimetric, however you want to. Or just stick with 2d isometric sprites all together. You might run into many more problems otherwise.
I used function this for rotation object in Unity 3D by AWSD key. You can refer.
void Update () {
float x = Input.GetAxis ("Horizontal") * Time.deltaTime * 150.0f;
float z = Input.GetAxis ("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate (0, x, 0);
transform.Translate (0, 0, z);
}

Rotate an object around its center point in unity 3d

How to rotate a 3d game object around its center point in unity 3d.
Just use bounds.center from the renderer
Vector3 position = myGameObject.GetComponent<Renderer>().bounds.center;
myGameObject.transform.RotateAround(position, rotationVector, degreesPerSecond * Time.deltaTime);
where rotationVector is your rotation axis (Vector3)
The two common ways to rotate an object are
The rotate attribute in the transform. Using this one you can set the exact coordinates for the target object rotation. However, you'll have to manage the smoothness by yourself if you want to make animations and the values are given through Quaternion type. I recommend to use the static method Quaternion.Euler so you can pass values in a X, Y, Z. The example below set the object to turn 90 degrees clockwise in the Y axis:
transform.rotation = Quaternion.Euler (0, 90, 0);
The second approach uses the Rotation method in the same transform attribute. This method allow you to pass the amount of degrees in which object will rotate and already accept X, Y and Z coordinates instead Quaternion.
The example below rotate the object 1 degree clockwise in the Y axis:
transform.Rotate (0, 1, 0);
To best understand the difference between both methods, if you use the first one in an Update method you'll see the object static rotated 90 degrees in the Y axis. The second example used in an Update will make the object spin clockwise in the Y axis (too fast).
use
Transform.rotation
look here for Examples/Documentation :
Unity Script Reference