While setting the Euler Angles property of an object changes its absolute rotation, there will often be times when you want to add an amount of rotation to an object instead.įor example, to turn an object round by 90 degrees: void RotateByDegrees() How to Rotate an object in Unity using the Rotate function While this works well for setting an absolute rotation, one time, it’s not as useful for adding rotation to an object.įor example, to rotate an object by a set amount of degrees from its current orientation to a new one.įor that, you’ll need the Rotate function. Setting the rotation of an object directly via its rotation property can be very straightforward. So while it’s generally better to use Quaternions when you can, there’s nothing wrong with using Euler Angles with Unity’s built-in rotation functions. Unity will convert the Euler value and work with Quaternions behind the scenes, avoiding the typical problems associated with Eulers, while keeping their ease of use. In practice, when using Unity’s built-in rotation functions, you shouldn’t need to worry about gimbal lock, even when passing in Euler Angles. Trying to work exclusively in Euler Angles can cause unexpected behaviour.įor example, the converted Euler Angle value can vary, since the same Quaternion rotation can be described using different Euler values, or you might reintroduce gimbal lock by trying to work only with Euler Angles in local variables. They make reading and manually setting the rotation of an object easier to do. It’s best to think of Euler Angles as a method of describing Quaternions in Unity. However, you may start to run into problems if you directly apply Euler Angle based rotation on a per axis basis or manually over time. However, Quaternions are complex and aren’t designed to be worked with directly.Įuler Angles, on the other hand, are easier to read and work with.īecause of this, if you want to manually set a rotation, or provide a rotation amount in degrees, then it will be much easier to use an Euler Angle value to do so, and you’ll likely find that there’s an Euler based function available for you to do just that. Generally speaking, Unity uses Quaternions because they’re efficient and avoid some of the problems of using Euler Angles, such as gimbal lock, which is the loss of a degree of movement when two axes are aligned the same way. While Unity uses Quaternions behind the scenes, many of Unity’s rotation functions will allow you to use either type of value, either natively or by converting the value. Should you use Quaternions or Euler Angles in Unity? The Euler Angles property is a convenient way to set and read an object’s rotation in a format that’s easier to read and work with than Quaternions. Like this: transform.rotation = Quaternion.Euler(new Vector3(0,10,0)) Or, instead of working in Quaternions, you can convert a Vector 3 Euler Angle rotation into a Quaternion. Like this: // Copies another object's rotation This means that if you want to set the rotation property directly, you’ll need to use a Quaternion value instead. So, when using lerAngles, you’re essentially setting and reading a converted value. The rotation value you see in the Inspector is the real Quaternion rotation converted to a Vector 3 value, an Euler Angle, which is rotation expressed as an XYZ value for pitch, yaw and roll. Instead, I set a different property, Euler Angles.īehind the scenes, Unity calculates rotation using Quaternions, meaning that the rotation property of an object is not actually a Vector 3, it’s a Quaternion. Notice, however, that I didn’t set the rotation property directly like I might do when setting an object’s position, for example. Rotational vector 3 values describe a degree of rotation around each of the 3 axes, X Y and Z. In this example, I’ve set the rotation of the object to 10 degrees around the Y Axis. Like this: Vector3 newRotation = new Vector3(0, 10, 0) Like this: Vector3 newPosition = new Vector3(0, 10, 0) Īn object’s rotation can also be set directly via its Transform, In the same way that a game object’s position in the world can be set using its Transform component… In the Inspector, you’ll see an object’s rotation as a Vector 3 value: Rotation in Unity typically works by specifying an amount of rotation in degrees around the X, Y or Z axis of an object. Which means that once you’re used to the basics of rotating an object in Unity, you’ll find it easier to use some of the more advanced rotation features that Unity offers. While there are a lot of different options available to you, many of them work in similar ways. While others work best for certain tasks. Some methods are simple, some are more complex. There are a lot of different ways to rotate an object in Unity. For a general overview of how to rotate in Unity, try my video, or continue to the full article below.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |