I have a transform which is being rotated around the X axis. But while transferring this rotation to another transform, i want it to do the exact same rotation. But then around the Z axis.
However, this rotation is not a "rotation" it is just a transform which rotation gets edited from the outside. So i have to rotate the original transform to match the 2nd transform, but take the rotation into account.
Axis in below images: RED = X, GREEN = Y, BLUE = Z
Rotation original:
Rotation it should have after:
What would be the correct way to go about this frame by frame?
Thanks in advance,
Smiley
I can give you an answer that solves the general problem of changing the rotation from one axis to another.
Each transform object has a transform.localRotation
property that represents the orientation of the transform, relative to the parent's orientation. If the transform has no parent, than this orientation is relative to World Space.
Quaternion
is the data type used to represent rotations, and it has a useful method called ToAngleAxis.
float angle;
Vector3 axis;
transform.localRotation.ToAngleAxis(out angle, out axis);
Will set the variables angle
and axis
to the angle and axis of localRotation
. The out
part means that you're passing in a variable meant to be set by the function, rather than to be used as input.
In this case, the angle
variable is the part you want to actually use. If you use Quaternion.AngleAxis, providing this angle
value and your desired axis, you'll get a new rotation that will match your desired parameters.
For example: To change any rotation of transform1
to a Z-axis rotation on transform2
, you would use
float angle;
float axis;
transform1.localRotation.ToAngleAxis(out angle, out axis);
transform2.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
Hope this helps