OscillatePosition.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Makes the object move back and forth in a cycle, on any axis you choose, relative to its starting local position.
  6. /// </summary>
  7. public class OscillatePosition : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// Curve that defines the pattern we move the transform over time.
  11. /// </summary>
  12. [Tooltip("Curve that defines the pattern we move the transform over time.")]
  13. public AnimationCurve moveCurve = new AnimationCurve(new Keyframe[5]
  14. {
  15. new Keyframe(0, 0, 5, 5),
  16. new Keyframe(0.25f, 1, 0, 0),
  17. new Keyframe(0.5f, 0, -5, -5),
  18. new Keyframe(0.75f, -1, 0, 0),
  19. new Keyframe(1, 0, 5, 5)
  20. });
  21. /// <summary>
  22. /// How far the transform travels on points on the curve equal to 1 or -1. (Technically not "max" but more intuitive than "normalized" or something.)
  23. /// </summary>
  24. [Tooltip("How far the transform travels on points on the curve equal to 1 or -1. (Technically not 'max' but more intuitive than 'normalized' or something.)")]
  25. public float maxDistance = 1f;
  26. /// <summary>
  27. /// How long it takes for a full cycle, ie. playing through moveCurve all the way.
  28. /// </summary>
  29. [Tooltip("How long it takes for a full cycle, ie. playing through moveCurve all the way.")]
  30. public float cycleTimeSeconds = 2f;
  31. /// <summary>
  32. /// Timer that keeps track of where we are in the moveCurve at any given moment.
  33. /// </summary>
  34. private float cycleTimer = 0f;
  35. /// <summary>
  36. /// True to move on the X axis.
  37. /// </summary>
  38. [Space(5)]
  39. [Tooltip("True to move on the X axis.")]
  40. public bool moveOnX = false;
  41. /// <summary>
  42. /// True to move on the Y axis.
  43. /// </summary>
  44. [Tooltip("True to move on the Y axis.")]
  45. public bool moveOnY = true;
  46. /// <summary>
  47. /// True to move on the Z axis.
  48. /// </summary>
  49. [Tooltip("True to move on the Z axis.")]
  50. public bool moveOnZ = false;
  51. /// <summary>
  52. /// Transform's localPosition at start. We move the transform relative to this each frame.
  53. /// </summary>
  54. private Vector3 startPosition;
  55. // Use this for initialization
  56. void Start ()
  57. {
  58. startPosition = transform.localPosition;
  59. }
  60. // Update is called once per frame
  61. void Update ()
  62. {
  63. cycleTimer += Time.deltaTime;
  64. if (cycleTimer > cycleTimeSeconds) cycleTimer %= cycleTimeSeconds; //Didn't know %= was a thing until now. :D
  65. float moveamount = moveCurve.Evaluate(cycleTimer / cycleTimeSeconds) * maxDistance;
  66. float xfinal = moveOnX ? startPosition.x + moveamount : transform.localPosition.x;
  67. float yfinal = moveOnY ? startPosition.y + moveamount : transform.localPosition.y;
  68. float zfinal = moveOnZ ? startPosition.z + moveamount : transform.localPosition.z;
  69. transform.localPosition = new Vector3(xfinal, yfinal, zfinal);
  70. }
  71. }