Home > Software engineering >  Unity3D, change only pitch in rotation of an object's transform
Unity3D, change only pitch in rotation of an object's transform

Time:11-27

Good evening. I have one GameObject, and I need to change its pitch (and do not impact on its yaw and roll). I have a quaternion which stores rotation from which I need to get the needed pitch and set to my game object. But the operation of cast from a Quaternion to Euler angles is not unique (one quaternion can be reperesented as multiple triples of Euler angles). Is it possible to do this without cast to Euler angles?

CodePudding user response:

Yes, either by manipulating the raw components of the quaternion (scary) or using one of unity's built-in functions for it

Quaternion.AngleAxis() - Probably what you're looking for, you can easily isolate the rotation to pitch only.

But there's lots more functions for similar things here under the "Static Methods" header.

CodePudding user response:

You can combine rotations like so (based on a Unity example), in this case the current rotation with a delta pitch rotation:

public class Example2 : MonoBehaviour
{
    float rotateSpeed = 90;

    // Applies a rotation of 90 degrees per second around the X axis for pitch
    void Update()
    {
        float angle = rotateSpeed * Time.deltaTime;
        transform.rotation *= Quaternion.AngleAxis(angle, Vector3.right);
    }
}
  • Related