Simple Rotation Script Free!

I have had this script for awhile now, but yesterday, I couldn’t handle it anymore. I was tired of always have to edit the script for each object that I wanted to rotate around a certain axis. The result is this little guy.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotator : MonoBehaviour
{
    //Rotational Speed
    public float speed = 0f;
   
    //Forward Direction
    public bool ForwardX = false;
    public bool ForwardY = false;
    public bool ForwardZ = false;
   
    //Reverse Direction
    public bool ReverseX = false;
    public bool ReverseY = false;
    public bool ReverseZ = false;
   
    void Update ()
    {
        //Forward Direction
        if(ForwardX == true)
        {
            transform.Rotate(Time.deltaTime * speed, 0, 0, Space.Self);
        }
        if(ForwardY == true)
        {
            transform.Rotate(0, Time.deltaTime * speed,  0, Space.Self);
        }
        if(ForwardZ == true)
        {
            transform.Rotate(0, 0, Time.deltaTime * speed, Space.Self);
        }
        //Reverse Direction
        if(ReverseX == true)
        {
            transform.Rotate(-Time.deltaTime * speed, 0, 0, Space.Self);
        }
        if(ReverseY == true)
        {
            transform.Rotate(0, -Time.deltaTime * speed,  0, Space.Self);
        }
        if(ReverseZ == true)
        {
            transform.Rotate(0, 0, -Time.deltaTime * speed, Space.Self);
        }
       
    }
}

It’s not much of a share, but it’s something back to the forum and those that need it.

11 Likes

Neat. Try a switch maybe (on an Enum)? :slight_smile:

Thanks! Didn’t think about doing that.

Anyone knows how to make a cube rotate 90 degrees just once? Im so lost.

    void Start()
    {
        transform.rotation = Quaternion.AngleAxis(90, Vector3.up);
    }

Just put on on the cube brother

4 Likes

anyone know how make charactere flip (backflip, frontflip…) ?

Use the forward X or reverse X checkbox in the OP’s script.

1 Like

Thank you for sharing those scripts ! They have helped me a lot!

1 Like

this is very cool thanks

you are awesome man tysm!

Great script man, kudos to you Sir!

THANK YOU, THANK YOU, THANK YOU! i am one of “those that need it” :slight_smile:

worked like a charm

Sigh*

Why is everybody so excited about this script and feels in need to revive such an ancient thread. Maybe read up on general forum rules and about necroposting. Anyways the script is unnecessarily complicated and even limited as you can only specify a single “speed” value, even though you can select multiple axis. Though that means you’re restricted to the major axis and the diagonals when combining 2 or 3 axes.

A way simpler and more flexible implementation would be

using UnityEngine;
public class Rotator : MonoBehaviour
{
    public Vector3 angularVelocity;
    public Space space = Space.Self;
    void Update ()
    {
        transform.Rotate(angularVelocity * Time.deltaTime, space);
    }
}

If you want to get even more fancier you could also add a boolean to replace deltaTime with its unscaled version which may come in handy for some static animations you want to continue to run when you pause your game with timescale.

    public bool useUnscaledDeltaTime = false;
    // [ ... ]
        transform.Rotate(angularVelocity * (useUnscaledDeltaTime?Time.unscaledDeltaTime:Time.deltaTime), space);

You can simply “invert” the rotation around an axis by simply using a negative number. Since you can specify all 3 axes seperately you can literally rotate around any axis you like.

Hopefully this thread is now done for good. If a future reader what to show his appreciation, just click the “like” button under a post, that’s enough and does not revive the thread.

3 Likes

Bit of an old post… but here is mine, you can choose the axis in the editor, and it has animation curve

    public Vector3 axis = Vector3.up;
    public float rate;
    public AnimationCurve curve;

    private void Update()
    {
        transform.localRotation = Quaternion.AngleAxis(curve.Evaluate(Time.time * rate) * 360, axis);
    }
1 Like

This is of course even more flexible, though one have to be more careful when setting up the animation curve. I would recommend to not use “Time.time” as in a long running game this can get unprecise, especially when you use a higher “rate”. I would create my own timer variable that wraps around after it passes 1.0.

timer += Time.deltaTime * rate;
while (timer > 1.0)
    timer -= 1.0;
// ...
2 Likes