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.
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.
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.
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.