Here are several practical C# examples of using quaternions in Unity.
1. Rotate an object to face a target
using UnityEngine;
public class FaceTarget : MonoBehaviour
{
public Transform target;
void Update()
{
if (target == null) return;
Vector3 direction = target.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = targetRotation;
}
}
2. Smoothly rotate toward a target
using UnityEngine;
public class SmoothFaceTarget : MonoBehaviour
{
public Transform target;
public float rotationSpeed = 5f;
void Update()
{
if (target == null) return;
Vector3 direction = target.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(
transform.rotation,
targetRotation,
rotationSpeed * Time.deltaTime
);
}
}
3. Rotate by a fixed angle
using UnityEngine;
public class RotateObject : MonoBehaviour
{
public float speed = 90f;
void Update()
{
transform.rotation *= Quaternion.Euler(0f, speed * Time.deltaTime, 0f);
}
}
4. Create a rotation from Euler angles
Quaternion rotation = Quaternion.Euler(30f, 90f, 0f);
transform.rotation = rotation;
5. Get Euler angles back
Vector3 angles = transform.rotation.eulerAngles;
Debug.Log($"X: {angles.x}");
Debug.Log($"Y: {angles.y}");
Debug.Log($"Z: {angles.z}");
6. Rotate around a local axis
using UnityEngine;
public class RollObject : MonoBehaviour
{
public float rollSpeed = 45f;
void Update()
{
transform.rotation *= Quaternion.AngleAxis(
rollSpeed * Time.deltaTime,
Vector3.forward
);
}
}
7. Rotate around a world axis
using UnityEngine;
public class WorldRotate : MonoBehaviour
{
public float speed = 90f;
void Update()
{
transform.Rotate(Vector3.up * speed * Time.deltaTime, Space.World);
}
}
8. Calculate the rotation between two directions
Vector3 from = Vector3.forward;
Vector3 to = Vector3.right;
Quaternion rotation = Quaternion.FromToRotation(from, to);
transform.rotation = rotation;
9. Clamp rotation speed
using UnityEngine;
public class RotateTowardsTarget : MonoBehaviour
{
public Transform target;
public float degreesPerSecond = 180f;
void Update()
{
if (target == null) return;
Quaternion targetRotation =
Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
targetRotation,
degreesPerSecond * Time.deltaTime
);
}
}
10. Blend between two rotations
Quaternion start = Quaternion.identity;
Quaternion end = Quaternion.Euler(0f, 180f, 0f);
float t = 0.5f;
Quaternion halfway = Quaternion.Lerp(start, end, t);
11. Rotate a direction vector
Quaternion rotation = Quaternion.Euler(0f, 90f, 0f);
Vector3 forward = rotation * Vector3.forward;
Debug.Log(forward); // (1, 0, 0)
12. Combine rotations
Quaternion yaw = Quaternion.Euler(0f, 45f, 0f);
Quaternion pitch = Quaternion.Euler(30f, 0f, 0f);
Quaternion combined = yaw * pitch;
transform.rotation = combined;
13. Reset rotation
transform.rotation = Quaternion.identity;
14. Check the angle between two rotations
float angle = Quaternion.Angle(
transform.rotation,
target.rotation
);
Debug.Log($"Angle Difference: {angle}");
15. Rotate around an arbitrary axis
Vector3 axis = new Vector3(1f, 1f, 0f).normalized;
Quaternion rotation = Quaternion.AngleAxis(45f, axis);
transform.rotation = rotation;
16. Make an object look only on the horizontal plane
using UnityEngine;
public class FaceTargetFlat : MonoBehaviour
{
public Transform target;
void Update()
{
if (target == null) return;
Vector3 direction = target.position - transform.position;
direction.y = 0f;
if (direction.sqrMagnitude > 0.001f)
{
transform.rotation = Quaternion.LookRotation(direction);
}
}
}
17. Rotate relative to the current orientation
Quaternion additionalRotation = Quaternion.Euler(0f, 30f, 0f);
transform.rotation = transform.rotation * additionalRotation;
18. Convert a quaternion to an axis-angle representation
Quaternion rotation = transform.rotation;
rotation.ToAngleAxis(out float angle, out Vector3 axis);
Debug.Log($"Angle: {angle}");
Debug.Log($"Axis: {axis}");
Common Quaternion Methods
| Method | Purpose |
|---|
Quaternion.identity | No rotation (0°, 0°, 0°). |
Quaternion.Euler() | Create a quaternion from Euler angles. |
Quaternion.LookRotation() | Face a direction. |
Quaternion.AngleAxis() | Rotate around an axis. |
Quaternion.FromToRotation() | Rotate from one direction to another. |
Quaternion.Lerp() | Linear interpolation between rotations. |
Quaternion.Slerp() | Smooth spherical interpolation. |
Quaternion.RotateTowards() | Rotate with a maximum angular speed. |
Quaternion.Angle() | Measure the difference between two rotations. |
Quaternion.Inverse() | Compute the opposite rotation. |
Quaternion.ToAngleAxis() | Convert a quaternion into an angle and axis. |
These examples cover the quaternion operations you'll use most often in Unity game development, including aiming, smooth turning, animation blending, camera control, and incremental rotations.