PlanetMover.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. /// <summary>
  6. /// Rotates this object around the specified center transform, as well as around itself on the specified axis.
  7. /// Used in the ZED planetarium sample on each planet/moon to make it orbit the sun and also spin on its own axis.
  8. /// </summary>
  9. public class PlanetMover : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// The transform this object revolves around. Always the Sun in the ZED planetarium sample, except for the moon.
  13. /// </summary>
  14. [Tooltip("The transform this object revolves around. Always the Sun in the ZED planetarium sample, except for the moon.")]
  15. public Transform center;
  16. /// <summary>
  17. /// Degrees per second this object moves around its orbit.
  18. /// </summary>
  19. [Tooltip("Degrees per second this object moves around its orbit.")]
  20. public float speedRevolution = 10;
  21. /// <summary>
  22. /// The axis of rotation around its poles, ie, the direction from the planet's south pole to the north pole.
  23. /// </summary>
  24. [Tooltip("The axis of rotation around its poles, ie, the direction from the planet's south pole to the north pole. ")]
  25. public Vector3 axis = Vector3.up;
  26. /// <summary>
  27. /// Degrees per second the object rotates on its own axis.
  28. /// </summary>
  29. [Tooltip("Degrees per second the object rotates on its own axis. ")]
  30. public float speed = 10.0f;
  31. /// <summary>
  32. /// Axis the planet revolves around on its orbit.
  33. /// </summary>
  34. private Vector3 dir;
  35. private void Start()
  36. {
  37. dir = center.up; //Get the axis of rotation from the object we're rotating.
  38. }
  39. // Update is called once per frame
  40. void Update () {
  41. transform.RotateAround(center.position, center.TransformDirection(dir), Time.deltaTime * speedRevolution); //Rotating around the sun (orbit).
  42. transform.Rotate(axis, speed*Time.deltaTime, Space.Self); //Rotating around its own axis (night/day).
  43. }
  44. }