Skip to content

Instantly share code, notes, and snippets.

@beardordie
Last active August 28, 2021 15:33
Show Gist options
  • Save beardordie/314bb5e743bcf477397ead0e85ac585e to your computer and use it in GitHub Desktop.
Save beardordie/314bb5e743bcf477397ead0e85ac585e to your computer and use it in GitHub Desktop.
Unity helper MonoBehaviour to apply constant World-space rotation to a GameObject
using UnityEngine;
public class ConstantRotation : MonoBehaviour
{
public bool randomizeValues;
public Vector3 speed = new Vector3(0f, 100f, 0f);
public float multiplier = 1;
public Vector2 randomMultiplierRange = new Vector2(90f, 120f);
void Start()
{
if (randomizeValues)
{
multiplier = Random.Range(randomMultiplierRange.x, randomMultiplierRange.y);
speed = Random.insideUnitSphere;
transform.eulerAngles = speed * 180f;
}
}
void Update()
{
Vector3 newRotation = (transform.right * Time.deltaTime * speed.x * multiplier) +
(transform.up * Time.deltaTime * speed.y * multiplier) +
(transform.forward * Time.deltaTime * speed.z * multiplier);
transform.Rotate(newRotation, Space.World);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment