CinemachineDollyCart.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using UnityEngine.Serialization;
  3. namespace Cinemachine
  4. {
  5. /// <summary>
  6. /// This is a very simple behaviour that constrains its transform to a CinemachinePath.
  7. /// It can be used to animate any objects along a path, or as a Follow target for
  8. /// Cinemachine Virtual Cameras.
  9. /// </summary>
  10. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  11. #if UNITY_2018_3_OR_NEWER
  12. [ExecuteAlways]
  13. #else
  14. [ExecuteInEditMode]
  15. #endif
  16. [DisallowMultipleComponent]
  17. [HelpURL(Documentation.BaseURL + "manual/CinemachineDollyCart.html")]
  18. public class CinemachineDollyCart : MonoBehaviour
  19. {
  20. /// <summary>The path to follow</summary>
  21. [Tooltip("The path to follow")]
  22. public CinemachinePathBase m_Path;
  23. /// <summary>This enum defines the options available for the update method.</summary>
  24. public enum UpdateMethod
  25. {
  26. /// <summary>Updated in normal MonoBehaviour Update.</summary>
  27. Update,
  28. /// <summary>Updated in sync with the Physics module, in FixedUpdate</summary>
  29. FixedUpdate,
  30. /// <summary>Updated in normal MonoBehaviour LateUpdate</summary>
  31. LateUpdate
  32. };
  33. /// <summary>When to move the cart, if Velocity is non-zero</summary>
  34. [Tooltip("When to move the cart, if Velocity is non-zero")]
  35. public UpdateMethod m_UpdateMethod = UpdateMethod.Update;
  36. /// <summary>How to interpret the Path Position</summary>
  37. [Tooltip("How to interpret the Path Position. If set to Path Units, values are as follows: 0 represents the first waypoint on the path, 1 is the second, and so on. Values in-between are points on the path in between the waypoints. If set to Distance, then Path Position represents distance along the path.")]
  38. public CinemachinePathBase.PositionUnits m_PositionUnits = CinemachinePathBase.PositionUnits.Distance;
  39. /// <summary>Move the cart with this speed</summary>
  40. [Tooltip("Move the cart with this speed along the path. The value is interpreted according to the Position Units setting.")]
  41. [FormerlySerializedAs("m_Velocity")]
  42. public float m_Speed;
  43. /// <summary>The cart's current position on the path, in distance units</summary>
  44. [Tooltip("The position along the path at which the cart will be placed. This can be animated directly or, if the velocity is non-zero, will be updated automatically. The value is interpreted according to the Position Units setting.")]
  45. [FormerlySerializedAs("m_CurrentDistance")]
  46. public float m_Position;
  47. void FixedUpdate()
  48. {
  49. if (m_UpdateMethod == UpdateMethod.FixedUpdate)
  50. SetCartPosition(m_Position + m_Speed * Time.deltaTime);
  51. }
  52. void Update()
  53. {
  54. float speed = Application.isPlaying ? m_Speed : 0;
  55. if (m_UpdateMethod == UpdateMethod.Update)
  56. SetCartPosition(m_Position + speed * Time.deltaTime);
  57. }
  58. void LateUpdate()
  59. {
  60. if (!Application.isPlaying)
  61. SetCartPosition(m_Position);
  62. else if (m_UpdateMethod == UpdateMethod.LateUpdate)
  63. SetCartPosition(m_Position + m_Speed * Time.deltaTime);
  64. }
  65. void SetCartPosition(float distanceAlongPath)
  66. {
  67. if (m_Path != null)
  68. {
  69. m_Position = m_Path.StandardizeUnit(distanceAlongPath, m_PositionUnits);
  70. transform.position = m_Path.EvaluatePositionAtUnit(m_Position, m_PositionUnits);
  71. transform.rotation = m_Path.EvaluateOrientationAtUnit(m_Position, m_PositionUnits);
  72. }
  73. }
  74. }
  75. }