Unity Simple 2D Rotation Script.

in tutorial •  7 years ago  (edited)

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:

public float degreesPerSec = 360f;

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:

float rotAmount = degreesPerSec * Time.deltaTime;
float curRot = transform.localRotation.eulerAngles.z;
transform.localRotation = Quaternion.Euler(new Vector3(0,0,curRot+rotAmount));

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 :

using UnityEngine; 
using System.Collections; 

public class Rotation : MonoBehaviour { 

    public float degreesPerSec = 360f; 

    void Start() { 
    } 

    void Update() { 
        float rotAmount = degreesPerSec * Time.deltaTime; 
        float curRot = transform.localRotation.eulerAngles.z; 
        transform.localRotation = Quaternion.Euler(new Vector3(0,0,curRot+rotAmount)); 
    } 

}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
http://weeklycoder.com/2015/07/20/unity-simple-2d-rotation-script/

@chrs944
Nice Job!
Keep the good work up!
Thanks for sharing

Thanks

Thanks mate, This actually helped me! ;)

Thanks!