Last active
April 22, 2020 15:49
-
-
Save Manamongods/0837d093600e9f0a32a4f13b6b884dd9 to your computer and use it in GitHub Desktop.
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
//Steffen Vetne made this | |
//Creative Commons 0 | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[ExecuteInEditMode] | |
public class Spinner : MonoBehaviour | |
{ | |
//Fields | |
public bool unscaledTime = false; | |
public bool worldSpace = false; | |
[Space(10)] | |
public Vector3 startRotation = Vector3.zero; | |
[Space(10)] | |
public Vector3 axis = Vector3.up; | |
[Space(10)] | |
public float rateMultiplier = 100; | |
public Vector2 randomRate = new Vector2(1, 2); | |
[Space(10)] | |
public Vector2 randomStartOffset = new Vector2(0, 0); | |
private float rate; | |
private Quaternion rotation; | |
//Lifecycle | |
#if UNITY_EDITOR | |
private void OnValidate() | |
{ | |
OnEnable(); | |
} | |
private void OnDrawGizmosSelected() | |
{ | |
float size = UnityEditor.HandleUtility.GetHandleSize(transform.position) * 0.75f; | |
Vector3 axis = this.axis; | |
if (transform.parent != null && !worldSpace) | |
axis = transform.parent.TransformDirection(axis); //Because it uses localRotation | |
Gizmos.DrawRay(transform.position, axis * size); | |
} | |
#endif | |
private void OnEnable() | |
{ | |
rate = Random.Range(randomRate.x, randomRate.y); | |
float offset = Random.Range(randomStartOffset.x, randomStartOffset.y); | |
rotation = Quaternion.AngleAxis(rateMultiplier * rate * offset, axis) * Quaternion.Euler(startRotation); | |
} | |
private void Update() | |
{ | |
float dt = unscaledTime ? Time.unscaledDeltaTime : Time.deltaTime; | |
rotation = Quaternion.AngleAxis(rateMultiplier * rate * dt, axis) * rotation; | |
if(worldSpace) | |
transform.rotation = rotation; | |
else | |
transform.localRotation = rotation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment