Manipulating Rotation in Unity

Admittedly, I’m mainly writing this article for myself because I always tend to forget.

The Transform component has 3 sets of coordinates. The “position” which tells the engine where the object is located in the game world, the “rotation” which tells the engine what direction the object is facing and finally, “scale” which tells the engine how large the object is.

When interacting with them via code the data types are as follows:

Vector3 position, Quaternion rotation, Vector3 scale

As seen above, the rotation is actually a Quaternion which is a data type that actually has a 4th argument called “w”. However, a Vector3 is shown in the inspector.

That means if we want to work with rotation the same way that we do in the inspector we need to be able to convert them back and forth. Luckily for us, a super smart dude named Leonhard Euler from the 1700s came up with a system for that. Double lucky for us because Unity has built in functions to help us implement his complex math!

Just remember it like this:

From Vector3 to Quaternion:

Quaternion rotation = Quaternion.Euler(someVector3)

From Quaternion to Vector3:

Vector3 position = someQuaternion.eulerAngles;

And boom! It’s that quick. Make sure you thank Leonhard Euler and Unity the next time you’re working with Quaternions!

Here’s a quick example of me rotating an asteroid in the GameDevHQ 2d Space Shooter project