Unity Simple 2D Rotation Script.

Unity Simple 2D Rotation Script.

Category : 2D , C# , Full Script , Game Development , Unity

While working in Unity I found it difficult to figure out how to get an object to rotate. If you’re reading this, you probably did too. Right now I’m just talking about 2D game object rotation. The concept works in 3D as well, just turn on a different axis, but I won’t go into that here.

If you don’t feel like reading the article and just want the Full Script… skip to the bottom.

Add this to the property section of your class:

This will be the variable that tells the script how fast to rotate around the axis. It’s in degrees per second. Feel free to play with the number as see how it affects the rotation speed.

Add this to the Update method of your class:

The first line just takes the degreesPerSec variable and multiplies by the delta time, to give us the fraction of degressPerSec to use this frame.

The second line gets the current rotation of the z-axis of the object. We use the z-axis because the x and y axis are being used to move left and right and up and down. Basically to make a object seen in 2 dimensions, have those dimensions appear to rotate, they must be turned in a 3rd dimension, but that’s a conversation for another time, just trust me and use the z-axis.

The third line resets the localRotation to a new Vector3, with 0 x rotation, 0 y rotation, and a z-axis rotation equal to the last z-axis rotation stored in curRot, added to rotAmount, the fraction of the degreesPerSec to rotate per frame.

Why use transform.localRotation instead of transform.rotation?
Well the idea here is that when you use transform.rotation, it returns the objects rotation in global space, and turn it, ow matter what the rotation of it’s parent is. Using localRotation turns the object with respect to it’s parent rotation. Feel free to interchange them, and view the effects, and see which one fits what you are trying to achieve.

What is Quaternion.eulerAngles?
Objects in Unity are rotated using a concept called a Quaternion. It allows objects to be rotated in any way in 3D space, and are very hard to visualize. They use x,y,z,and w, and some imaginary number i,j, and k to work. Luckily they can be created using Euler Angles which is what we all know and love, and the method I use in the example. If you want to know more about Quaternions, don’t ask me, watch this helpful youtube video.

This guy knows what he’s talking about.


Full Script :


Liked it? Take a second to support Justin Fletcher on Patreon!
The following two tabs change content below.
Game Designer, Programmer, Lover of all things retro. I've been a programmer for about 8 years. Got into game development part time 6 years ago and haven't been able to put it down.

Latest posts by Justin Fletcher (see all)

About Author

Justin Fletcher

Game Designer, Programmer, Lover of all things retro. I've been a programmer for about 8 years. Got into game development part time 6 years ago and haven't been able to put it down.



May 30, 2017 at 8:56 am

Basic functions for when you just need to move something from point A to point B, or rotate by a given number of degrees, without anything fancy.


May 19, 2019 at 9:59 am

How do you change the rotation from counterclockwise to clockwise?

    Justin Fletcher

    May 21, 2019 at 8:40 am

    take ‘rotAmount’ and multiply it by -1
    rotAmount *= -1;

Leave a Reply to Justin Fletcher Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Support Us

Support The Weekly Coder on Patreon!


Recent Popular Info

This channel is all about coding. We will mostly post tutorials on making games with Unity 5 and above. Other posts will include tutorials on specific game mechanics and whatever else we feel like talking about.

Don't be shy.. If you have a question, leave a comment. If you you'd like to see a specific tutorial feel free to request it. If we think its worthwhile, then we may just do it.

Please don't forget to click the like button and remember to subscribe. Feel free to support our channel and the awesome content we create.

Total subscribers:
1.9 thousand

Total uploads:

Total upload views:
250.2 thousand

Follow me on Twitter


RSS Feed

Blog Authors