CinemachineDollyCart.cs 3.7 KB

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