Last active
August 28, 2021 15:33
-
-
Save beardordie/314bb5e743bcf477397ead0e85ac585e to your computer and use it in GitHub Desktop.
Unity helper MonoBehaviour to apply constant World-space rotation to a GameObject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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