Oscillator.cs 527 B

123456789101112131415161718192021
  1. using UnityEngine;
  2. // Makes a transform oscillate relative to its start position
  3. public class Oscillator : MonoBehaviour
  4. {
  5. public float m_Amplitude = 1.0f;
  6. public float m_Period = 1.0f;
  7. public Vector3 m_Direction = Vector3.up;
  8. Vector3 m_StartPosition;
  9. void Start()
  10. {
  11. m_StartPosition = transform.position;
  12. }
  13. void Update()
  14. {
  15. var pos = m_StartPosition + m_Direction * m_Amplitude * Mathf.Sin(2.0f * Mathf.PI * Time.time / m_Period);
  16. transform.position = pos;
  17. }
  18. }