Changing rotation
- Game Development course
- Beginner
- Lesson 40/83
- 4 min
Learn to use Quaternion with transform.rotation.
Rotation with Transform
transform.rotation stores an object's rotation as a Quaternion. Quaternion is a data structure used to represent rotations in three-dimensional space. Instead of assigning angle values directly, you can create a Quaternion from degree-based angles using Quaternion.Euler.
transform.rotation = Quaternion.Euler(0, 90, 0);This statement rotates the object by 90 degrees around the Y axis.
Quaternion and Euler Angles
Euler angles express the amount of rotation around the X, Y, and Z axes in degrees. Quaternion.Euler(x, y, z) converts these angles into a Quaternion value. You can assign the resulting value to the transform.rotation property.
Quaternion turn = Quaternion.Euler(30, 60, 0);
transform.rotation = turn;Quaternion represents rotations, while Euler angles make it easier to work with readable degree values.
Practice Rotate the Object to a Specific Angle
In the Start method, set the object's rotation to 0 degrees around X, 75 degrees around Y, and 0 degrees around Z. Assign the result of Quaternion.Euler to transform.rotation.
Starter code
using UnityEngine;
public class RotationSetter : MonoBehaviour
{
void Start()
{
// Set the required rotation.
}
}Sample solution
using UnityEngine;
public class RotationSetter : MonoBehaviour
{
void Start()
{
transform.rotation = Quaternion.Euler(0, 75, 0);
}
}Expected output
(0.00, 75.00, 0.00)
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which statement correctly rotates an object by 45 degrees around the Y axis?
- transform.rotation = Quaternion.Euler(0, 45, 0); Correct answer
- transform.position = Quaternion.Euler(0, 45, 0);
- transform.Translate(0, 45, 0);
- transform.rotation = new Vector3(0, 45, 0);
Explanation: Quaternion.Euler converts three angles given in degrees into a Quaternion rotation value.
2 Create a Quaternion Rotation
transform.rotation = Quaternion.Euler(0, ____, 0);
transform.rotation = Quaternion.Euler(0, 120, 0);
Explanation: The second parameter of the Euler method specifies the angle around the Y axis. This example rotates the object by 120 degrees around Y.
3 Rotation Concepts
| Term | Meaning |
|---|---|
transform.rotation |
Stores object's Quaternion rotation |
Quaternion |
Represents 3D rotations |
Quaternion.Euler |
Converts angles to a Quaternion |
Euler açıları |
Specify X, Y, Z rotations in degrees |