LocalRotator.cs 952 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class LocalRotator : MonoBehaviour
  5. {
  6. public float MaxAngle = 15;
  7. public AnimationCurve Curve;
  8. public float LoopLength;
  9. public float Offset;
  10. public Vector3 Axis = Vector3.up;
  11. Quaternion startRotation;
  12. float elapsedTime;
  13. private void Start()
  14. {
  15. startRotation = transform.localRotation;
  16. elapsedTime = Offset;
  17. }
  18. private void OnEnable()
  19. {
  20. elapsedTime = Offset;
  21. }
  22. private void Update()
  23. {
  24. if(float.IsNaN(elapsedTime))
  25. {
  26. elapsedTime = 0;
  27. }
  28. elapsedTime += Time.deltaTime;
  29. elapsedTime = Mathf.Repeat(elapsedTime, LoopLength);
  30. float fraction = elapsedTime / LoopLength;
  31. float value = Curve.Evaluate(fraction);
  32. transform.localRotation = Quaternion.AngleAxis(value * MaxAngle, Axis) * startRotation;
  33. }
  34. }