Get Your Personalized Game Dev Plan Tailored tips, tools, and next steps - just for you.

Making Sense of Quaternion.Identity in Unity

Posted by Gemma Ellison
./
August 24, 2023
The cover for Making Sense of Quaternion.Identity in Unity

In Unity, you use Quaternions to describe rotations. In this guide, I’ll overview what a Quaternion is. And I will explain why you use this property.

What you need to know

  • A quaternion represents a rotation in 3D space.
  • The identity is a value. This value represents the idea of a neutral orientation.
  • You will use this value when you instantiate game objects.
  • You will also use this value to set the transform.rotation value.

In this article, I will show you two examples where you will use this value.

Then, I’ll give more context about what a quaternion is. I’ll explain how a Quaternion represents a rotation. And I’ll describe how you can convert a quaternion to an Euler angle. I’ll do all this, focusing on the identity rotation.

You must understand how instantiate works before reading the rest of the article.

Use Quaternion.identity to instantiate an object

This section will give two examples. Each example will show a different way to use the identity Quaternion in Unity. You will use this value in one of two scenarios. I encourage you to follow along.

Scenario 1: Instantiate a Game Object with Quaternion.identity

This example will show how to create a new game object. When we create the game object, we will use the “no rotation” value. This identity value helps us create the object with a neutral rotation.

Here’s how we will accomplish this:

We will make a new game object. We will use the Instantiate method. In the instantiate method, we specify a rotation. We use the identity property to describe a " none " rotation amount.


using UnityEngine;

‍

public class InstantiateExample : MonoBehaviour

{

    public GameObject objectToCreate = null;

‍

    void Start()

    {

        Instantiate(objectToCreate, transform.position, Quaternion.identity);

    }

}

Scenario 2: Instantiate a GameObject with a rotation

In this scenario, we create a new game object. In scenario 1, we set the game object to the identity rotation. In this scenario, we set the game object to the transform rotation.

Let’s create a new game object using Instantiate. Use the rotation of the spawning object to set the rotation of our new instanced object. Our new object will inherit the spawner’s rotation.

I will give a concrete example. Imagine that you are making a first-person shooter. You want to create a bullet at your gun barrel when the player clicks. Then, the shell moves forward every frame. To do this, you instantiate a new shot. Would you spawn the bullet with the Quaternion.identity or transform.rotation?

Let us imagine that you use Quaternion.identity. No matter where you point your gun barrel, every bullet will face the same direction.

Instead, you use transform.rotation. When you spawn a bullet, Unity will align its forward vector with the direction of the gun.

Create a free account, or log in.

Gain access to free articles, game development tools, and game assets.