3

I am learning how to use unity and just had a quick question about the best practice when altering certain aspects of rotation. If I only wanted to adjust the x and z aspects of a new rotation what would be the best way to do it.

I came up with either:

newRotation.x = 0F;
newRotation.z = 0F;

Or:

newRotation = new Quaternion(0f, newRotation.y, 0f, newRotation.w);

Which of these would be the better way to go about accomplishing the task of zeroing out x and z but leaving the rest the same? Or is there another way that I am missing?

Thanks

Steven
  • 166,672
  • 24
  • 332
  • 435
  • 2
    If `newRotation` is a local variable, it probably doesn't matter; if it's a class field, I'd go with whichever allocates less heap (see [this SO thread about struct allocation](http://stackoverflow.com/questions/203695/does-using-new-on-a-strict-allocate-it-on-the-heap-or-stack) for reference). As an aside, are you *quite* sure you know what that does? If you're not sure what a quaternion's internal values represent, you almost certainly want to be working with [euler angles](http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-eulerAngles.html), instead. – rutter Jan 18 '14 at 00:26

1 Answers1

0

By changing x,y or z I assume you are trying to change your rotation axis.

It is important to keep in mind that these values represent the coordinates of the rotation axis multiplied by the sin of half the rotation angle. If you are not careful when setting them you might set a different rotation.

A safe way to change the rotation is to use Quaternion(Vector3D, Double), where you set your rotation axis and angle in degrees.