StepRotation.cs 840 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. This script rotates an object in smoothed increments.
  3. Email :: thomas.ir.rasor@gmail.com
  4. */
  5. using UnityEngine;
  6. public class StepRotation : MonoBehaviour
  7. {
  8. public int segments = 4;
  9. public float delay = 0.25f;
  10. float i = 0f;
  11. float t = 0f;
  12. Quaternion targetRotation = Quaternion.identity;
  13. void Update ()
  14. {
  15. transform.rotation = Quaternion.SlerpUnclamped( transform.rotation , targetRotation , Time.deltaTime * 10f );
  16. t += Time.deltaTime;
  17. if (t >= delay)
  18. {
  19. t = 0f;
  20. Rotate();
  21. }
  22. }
  23. public void Rotate()
  24. {
  25. i = Mathf.Repeat( i + 1f / segments , 1f );
  26. float theta = i * Mathf.PI * 2f;
  27. targetRotation = Quaternion.LookRotation( new Vector3( Mathf.Sin( theta ) , 0f , Mathf.Cos( theta ) ) , Vector3.up );
  28. }
  29. }