PingPong.cs 974 B

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Devdog.SciFiDesign.UI
  4. {
  5. public class PingPong : MonoBehaviour
  6. {
  7. public Vector2 scaleAxis = new Vector2(1f, 0f);
  8. public float scaleMin = 5f;
  9. public float scaleMax = 10f;
  10. public float speedMin = 5f;
  11. public float speedMax = 10f;
  12. private float _scale;
  13. private float _speed;
  14. private float _randomOffset;
  15. private RectTransform _rectTransform;
  16. private void Awake()
  17. {
  18. _rectTransform = GetComponent<RectTransform>();
  19. _randomOffset = Random.value;
  20. _speed = Random.Range(speedMin, speedMax);
  21. _scale = Random.Range(scaleMin, scaleMax);
  22. }
  23. protected void Update()
  24. {
  25. var val = Mathf.PingPong(Time.time * _speed + _randomOffset, _scale);
  26. _rectTransform.anchoredPosition = new Vector2(val*scaleAxis.x, val*scaleAxis.y);
  27. }
  28. }
  29. }