RotateOnAxes.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Rotates the object along its axes consistently.
  6. /// Because its using local axes, it affects the angle of other axes going forward, resulting in wild
  7. /// but possibly desired behavior if multiple axes are rotating at once.
  8. /// Used in the ZED Dark Room example scene to rotate lights.
  9. /// </summary>
  10. public class RotateOnAxes : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// How far it spins on the X axis (pitch) per frame.
  14. /// </summary>
  15. [Tooltip("How far it spins on the X axis (pitch) per frame. ")]
  16. public float xRevolutionsPerSecond = 0.5f;
  17. /// <summary>
  18. /// How far it spins on the Y axis (yaw) per frame.
  19. /// </summary>
  20. [Tooltip("How far it spins on the Y axis (yaw) per frame. ")]
  21. public float yRevolutionsPerSecond = 0.25f;
  22. /// <summary>
  23. /// How far it spins on the Z axis (roll) per frame.
  24. /// </summary>
  25. [Tooltip("How far it spins on the Z axis (roll) per frame. ")]
  26. public float zRevolutionsPerSecond = 0;
  27. // Update is called once per frame
  28. void Update ()
  29. {
  30. //Rotate on the axes. Note that the order this occurs is important as each rotation changes transform.localRotation.
  31. transform.Rotate(transform.localRotation * Vector3.right, xRevolutionsPerSecond * 360 * Time.deltaTime); //Pitch
  32. transform.Rotate(transform.localRotation * Vector3.up, yRevolutionsPerSecond * 360 * Time.deltaTime); //Yaw
  33. transform.Rotate(transform.localRotation * Vector3.forward, zRevolutionsPerSecond * 360 * Time.deltaTime); //Roll
  34. }
  35. }