RotatePingPong.cs 933 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Devdog.SciFiDesign.UI
  4. {
  5. public class RotatePingPong : MonoBehaviour
  6. {
  7. public Vector3 rotationMin;
  8. public Vector3 rotationMax;
  9. public AnimationCurve curve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
  10. public float speed = 0.2f;
  11. public bool randomizeSpeed = false;
  12. public float maxSpeed = 1f;
  13. private float _speed = 0f;
  14. protected void Awake()
  15. {
  16. _speed = speed;
  17. if (randomizeSpeed)
  18. {
  19. _speed = Random.Range(speed, maxSpeed);
  20. }
  21. }
  22. protected void Update()
  23. {
  24. var a = Vector3.Lerp(Vector3.zero, rotationMax - rotationMin, curve.Evaluate(Mathf.PingPong(Time.time * _speed, 1f)));
  25. a += rotationMin;
  26. transform.localRotation = Quaternion.Euler(a);
  27. }
  28. }
  29. }