How to Rotate Object in Unity: Simple Guide with Examples
In Unity, you rotate an object by changing its
Transform.rotation or using Transform.Rotate() method. You can specify rotation angles in degrees around the x, y, and z axes to spin the object as needed.Syntax
Unity provides two main ways to rotate an object:
transform.Rotate(Vector3 eulerAngles): Rotates the object by the given angles in degrees relative to its current rotation.transform.rotation = Quaternion.Euler(float x, float y, float z): Sets the object's rotation to the exact angles specified.
Here, Vector3 holds rotation angles for x, y, and z axes. Quaternion.Euler converts these angles into a rotation format Unity uses internally.
csharp
transform.Rotate(Vector3 eulerAngles); transform.rotation = Quaternion.Euler(x, y, z);
Example
This example rotates a GameObject 90 degrees around the Y axis every second when the game runs.
csharp
using UnityEngine; public class RotateObject : MonoBehaviour { void Update() { // Rotate 90 degrees per second around Y axis transform.Rotate(0, 90 * Time.deltaTime, 0); } }
Output
The object smoothly spins around its vertical axis, completing a full rotation every 4 seconds.
Common Pitfalls
1. Using degrees vs radians: Unity rotation methods use degrees, not radians. Passing radians will cause unexpected rotation.
2. Overwriting rotation every frame: Setting transform.rotation directly in Update() without accumulating rotation can freeze the object’s rotation.
3. Confusing local vs world rotation: transform.Rotate() rotates relative to local axes by default. Use Space.World to rotate around global axes.
csharp
/* Wrong: Overwrites rotation every frame, no smooth spin */ void Update() { transform.rotation = Quaternion.Euler(0, 90, 0); } /* Right: Adds rotation smoothly */ void Update() { transform.Rotate(0, 90 * Time.deltaTime, 0); }
Quick Reference
| Method | Description | Usage Example |
|---|---|---|
| transform.Rotate(Vector3 eulerAngles) | Rotates object by given angles relative to current rotation | transform.Rotate(0, 45, 0); |
| transform.Rotate(Vector3 eulerAngles, Space.World) | Rotates object around global axes | transform.Rotate(0, 45, 0, Space.World); |
| transform.rotation = Quaternion.Euler(x, y, z) | Sets object rotation to exact angles | transform.rotation = Quaternion.Euler(0, 90, 0); |
Key Takeaways
Use transform.Rotate() to smoothly add rotation relative to current orientation.
Use Quaternion.Euler() to set an exact rotation angle.
Rotation angles are in degrees, not radians.
Remember to use Space.World if you want to rotate around global axes.
Avoid overwriting rotation every frame without accumulation to keep smooth motion.