PlayerMove.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using Cinemachine.Utility;
  3. using UnityEngine;
  4. namespace Cinemachine.Examples
  5. {
  6. public class PlayerMove : MonoBehaviour
  7. {
  8. public float Speed;
  9. public float VelocityDamping;
  10. public float JumpTime;
  11. public enum ForwardMode
  12. {
  13. Camera,
  14. Player,
  15. World
  16. };
  17. public ForwardMode InputForward;
  18. public bool RotatePlayer = true;
  19. public Action SpaceAction;
  20. public Action EnterAction;
  21. Vector3 m_currentVleocity;
  22. float m_currentJumpSpeed;
  23. float m_restY;
  24. private void Reset()
  25. {
  26. Speed = 5;
  27. InputForward = ForwardMode.Camera;
  28. RotatePlayer = true;
  29. VelocityDamping = 0.5f;
  30. m_currentVleocity = Vector3.zero;
  31. JumpTime = 1;
  32. m_currentJumpSpeed = 0;
  33. }
  34. private void OnEnable()
  35. {
  36. m_currentJumpSpeed = 0;
  37. m_restY = transform.position.y;
  38. SpaceAction -= Jump;
  39. SpaceAction += Jump;
  40. }
  41. void Update()
  42. {
  43. #if ENABLE_LEGACY_INPUT_MANAGER
  44. Vector3 fwd;
  45. switch (InputForward)
  46. {
  47. case ForwardMode.Camera:
  48. fwd = Camera.main.transform.forward;
  49. break;
  50. case ForwardMode.Player:
  51. fwd = transform.forward;
  52. break;
  53. case ForwardMode.World:
  54. default:
  55. fwd = Vector3.forward;
  56. break;
  57. }
  58. fwd.y = 0;
  59. fwd = fwd.normalized;
  60. if (fwd.sqrMagnitude < 0.01f)
  61. return;
  62. Quaternion inputFrame = Quaternion.LookRotation(fwd, Vector3.up);
  63. Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  64. input = inputFrame * input;
  65. var dt = Time.deltaTime;
  66. var desiredVelocity = input * Speed;
  67. var deltaVel = desiredVelocity - m_currentVleocity;
  68. m_currentVleocity += Damper.Damp(deltaVel, VelocityDamping, dt);
  69. transform.position += m_currentVleocity * dt;
  70. if (RotatePlayer && m_currentVleocity.sqrMagnitude > 0.01f)
  71. {
  72. var qA = transform.rotation;
  73. var qB = Quaternion.LookRotation(
  74. (InputForward == ForwardMode.Player && Vector3.Dot(fwd, m_currentVleocity) < 0)
  75. ? -m_currentVleocity
  76. : m_currentVleocity);
  77. transform.rotation = Quaternion.Slerp(qA, qB, Damper.Damp(1, VelocityDamping, dt));
  78. }
  79. // Process jump
  80. if (m_currentJumpSpeed != 0)
  81. m_currentJumpSpeed -= 10 * dt;
  82. var p = transform.position;
  83. p.y += m_currentJumpSpeed * dt;
  84. if (p.y < m_restY)
  85. {
  86. p.y = m_restY;
  87. m_currentJumpSpeed = 0;
  88. }
  89. transform.position = p;
  90. if (Input.GetKeyDown(KeyCode.Space) && SpaceAction != null)
  91. SpaceAction();
  92. if (Input.GetKeyDown(KeyCode.Return) && EnterAction != null)
  93. EnterAction();
  94. #else
  95. InputSystemHelper.EnableBackendsWarningMessage();
  96. #endif
  97. }
  98. public void Jump()
  99. {
  100. m_currentJumpSpeed += 10 * JumpTime * 0.5f;
  101. }
  102. }
  103. }