Quaternion
TypeStruct (value type)
NamespaceUnityEngine
Used byTransform.rotation, Instantiate, Rigidbody.MoveRotation

Quaternion

A Quaternion stores a rotation. Internally it's four numbers (x, y, z, w) that you should never edit directly. Instead, you create and combine rotations with the helper methods on this page. You can think in degrees and let Unity do the math.

Describing a rotation as three angles (X, Y, Z) is like steering with three dials, one per axis. It works most of the time, but in some positions two dials end up turning the same way and you lose one direction of control. This is called gimbal lock. A quaternion is more like saying "point along this arrow, then twist this much". It has no dials, so nothing can jam. You don't need to understand the four numbers, just like you don't need to understand a car engine to drive.

Static Properties

NameDescription
Quaternion.identity"No rotation". Use it when spawning something that shouldn't be rotated.

Properties

NameTypeDescription
eulerAnglesVector3The rotation as X/Y/Z degrees. Reading it can give surprising (but equivalent) numbers.

Static Methods

MethodReturnsDescription
Euler(x, y, z)QuaternionMake a rotation from degrees. The most common way to create one.
AngleAxis(angle, axis)QuaternionRotate angle degrees around an axis, e.g. AngleAxis(90, Vector3.up).
LookRotation(forward, up = Vector3.up)QuaternionA rotation that faces a direction.
FromToRotation(from, to)QuaternionThe rotation that turns one direction into another. E.g. align an object with a slope.
RotateTowards(from, to, maxDegrees)QuaternionTurn toward a target by at most maxDegrees. Constant turn speed, never overshoots.
Slerp(a, b, t)QuaternionA rotation part-way between a and b. Smooth blending.
Angle(a, b)floatDegrees between two rotations.
Inverse(q)QuaternionThe opposite rotation.

Operators

CodeMeaning
a * bCombine: first apply b, then a. Order matters: a * b is usually not the same as b * a.
q * vectorRotate a Vector3 by q. E.g. transform.rotation * Vector3.forward equals transform.forward.

Code Samples

Setting a rotation with degrees

transform.rotation = Quaternion.Euler(0f, 90f, 0f); // face +X (turned right 90 degrees)

Turn smoothly to face a target (turret)

using UnityEngine;

public class SmoothTurret : MonoBehaviour
{
    [SerializeField] private Transform target;
    [SerializeField] private float turnSpeed = 90f; // degrees per second

    void Update()
    {
        Vector3 dir = target.position - transform.position;
        dir.y = 0f; // stay level: only turn left/right
        if (dir == Vector3.zero) return;

        Quaternion wanted = Quaternion.LookRotation(dir);
        transform.rotation = Quaternion.RotateTowards(
            transform.rotation, wanted, turnSpeed * Time.deltaTime);
    }
}

Spawn a bullet with a random spread

Quaternion spread = Quaternion.Euler(Random.Range(-3f, 3f), Random.Range(-3f, 3f), 0f);
Instantiate(bulletPrefab, muzzle.position, muzzle.rotation * spread);

Keep your own angle instead of reading eulerAngles

using UnityEngine;

public class MouseLook : MonoBehaviour
{
    [SerializeField] private float sensitivity = 2f;
    private float pitch; // up/down, kept by us so it never "flips"
    private float yaw;   // left/right

    void Update()
    {
        yaw   += Input.GetAxis("Mouse X") * sensitivity;
        pitch -= Input.GetAxis("Mouse Y") * sensitivity;
        pitch  = Mathf.Clamp(pitch, -80f, 80f); // don't look past straight up/down

        transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
    }
}

What happens at runtime

  1. Quaternion.Euler(x, y, z) converts your degrees into a quaternion. Unity applies them in the order Z, then X, then Y.
  2. When you read eulerAngles, Unity converts back. Many different sets of angles describe the same rotation, so you might get (180, 0, 180) where you expected (0, 180, 0). Both are correct; they're just two ways of writing it.
  3. Like Vector3, Quaternion is a struct: it's copied on assignment and creates no garbage.

Common Mistakes

See also