Facebook

Making Sense of Quaternion.Identity in Unity

by Michael Sacco Published 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.


Instantiate(objectToCreate, transform.position, transform.rotation);

How do I use quaternion identity to rotate an existing object?

Scenario 1: Set the transform rotation to Quaternion.identity

In this scenario, I set the rotation of the game object to the quaternion identity rotation. Add this script to an object in your scene. In Edit mode, rotate the object. Then, run your game.

What do you expect will happen when you hit play?

Does the result match up with your expectations?


using UnityEngine;

public class SetRotationExample : MonoBehaviour

{

    void Start()

    {

        transform.rotation = Quaternion.identity;

    }

}

Scenario 2: Rotate the transform using the Quaternion identity

In this scenario, I will give a Quaternion.identity rotation example. You can use the * operator with a Quaternion. This operator multiplies one Quaternion with another. You can think of Quaternion multiplication the same as Euler angle addition. In this example, we “add” the identity rotation to our object using the * operator. Observe what happens to the object’s rotation.


using UnityEngine;



public class ApplyRotationExample : MonoBehaviour

{

    void Start()

    {

        transform.rotation = transform.rotation \* Quaternion.identity;

    }

}

What does Quaternion mean in Unity?

The Quaternion is one way to represent a rotation. Unity uses quaternions for all rotation math. You must understand that a quaternion encodes a particular rotation.

Quaternion is a struct in Unity. To construct it, you use the constructor:


Quaternion newQuaternion = new Quaternion(1, 0, 0, 1);

What rotation do you think this Quaternion represents?

In general, you do not need to construct quaternions by hand. Usually, you will create an Euler angle. Then, convert the Euler angle to a quaternion. In Unity, it is easy to go from Quaternion to Euler with one simple method.

How to convert Quaternion to Euler

It is straightforward to convert Quaternion to Euler in Unity. Call the .eulerAngles property of the Quaternion.

I will illustrate a property call. This property creates an Euler angle. This angle represents the current Quaternion of the transform.


Vector3 eulerAngle = transform.rotation.eulerAngles;

You can use the .eulerAngles property on a Quaternion. This property returns the Euler angle encoding that corresponds to the Quaternion rotation. What do you expect the Euler angle representation to look like? Write down what you think. Then, try running this code.


Debug.Log(Quaternion.identity.eulerAngles);

Did the result match your expectations?

How to convert Euler to Quaternion.

It is also easy to convert from an Euler angle to a quaternion in Unity. Use the Quaternion.Euler(x,y,z) method.

For example, this will create a quaternion that represents a rotation of 180, 0, 0.


Quaternion quaternion = Quaternion.Euler(180, 0, 0);

You can use the Quaternion.Euler method to construct the identity rotation. Based on your understanding so far, what would you expect to use for the input of the Euler values? Write down what you think. Then, try running this code.


Quaternion identity = Quaternion.Euler(0,0,0);

Debug.Log($”The quaternion we created is {identity}. But, the identity quaternion is {Quaternion.identity}. Are these the same?”);

Can I avoid entering the rotation?

By default, no. Unity has a few declarations for the Instantiate method. If you want to provide the position, you must also provide the rotation.

I wrote a helper method that sets the rotation for you.

Add this script to your project.


using UnityEngine;



public static class EasyInstantiate

{

    public static GameObject Instantiate(GameObject original, Vector3 position)

    {

        return Object.Instantiate(original, position, Quaternion.identity);

    }

}

Use it like this:


using UnityEngine;



public class UseEasyInstantiate : MonoBehaviour

{

    void Start()

    {

        GameObject newObject = EasyInstantiate.Instantiate(gameObject, transform.position);

    }

}

This way, you can avoid writing Quaternion.identity

Recommended reading

Resources

https://docs.unity3d.com/ScriptReference/Quaternion-identity.html

https://forum.unity.com/threads/quaternion-identity-what-does-identity-mean.894253/

https://en.wikipedia.org/wiki/Quaternion

https://stackoverflow.com/questions/58780952/what-is-the-difference-between-a-unit-quaternion-and-an-identity-quaternion

https://vionixstudio.com/2022/06/16/unity-quaternion-and-rotation-guide/

FAQs

How can I represent a rotation?

There are three ways to represent a 3D rotation.

  • Quaternion
  • Euler angle
  • Matrix

Does Unity use Quaternions?

Yes. Unity uses quaternions to represent rotations. As a developer, you usually use Euler Angles to describe a rotation. Under the hood, Unity uses this type to represent all rotations. So, it translates your Euler Angle rotation to a Quaternion. Then, Unity uses that value.

Does Unreal use Quaternions?

Yes. Unreal Engine uses quaternions to represent rotations as well. It’s the same story with Unity. As a developer, you rarely work with Quaternions. They’re tricky. So, you will use an Euler angle to represent a rotation. In general, Unreal functions use quaternions.

Is the Identity Quaternion the Default Rotation?

Yes, in a sense. Imagine that you create a new GameObject in Unity. Unity initializes its rotation to the identity Quaternion. Unity applies “no rotation” to the object. The forward vector matches the forward vector of the imported model.

Why Use Quaternion.Identity During Instantiate?

Unity offers a few method declarations for the Instantiate method. Some of these declarations allow you to specify a position for the new object. All those declarations also need a rotation. And the rotation expects a Quaternion type. It’s how Unity designed the instantiate method.

Conclusion

As you can see, the identity quaternion is super helpful. You’re creating and using rotations in Unity. This article gave you a brief overview of what a Quaternion is and why you use Quaternion identity. I also gave you three examples where you can use this value.

Continue your learning by polishing up your math skills with our comprehensive guide to absolute value: /blog/absolute-value-in-unity-a-comprehensive-overview

Then, familiarize yourself with the Lerp method in Unity. /blog/using-lerp-in-unity

Improve your familiarity with fundamental unity concepts. You will develop better games in the long run.

Free download: Indie Game Marketing Checklist

Download now

Category

unity basics

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free