PlayerMovement.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class PlayerMovement : MonoBehaviour
  4. {
  5. [Header("Control Mode")]
  6. [Tooltip("Is this player controlled by human input or AI?")]
  7. public bool isPlayerControlled = false;
  8. [Header("Movement Settings")]
  9. public float moveSpeed = 5f;
  10. public float rotationSpeed = 10f;
  11. public float maxSpeed = 8f;
  12. public float acceleration = 15f;
  13. public float deceleration = 10f;
  14. private Vector2 moveInput;
  15. private PlayerInput playerInput;
  16. private Rigidbody rb;
  17. private PlayerAIMovement aiMovement;
  18. private PlayerController playerController;
  19. void Awake()
  20. {
  21. playerInput = GetComponent<PlayerInput>();
  22. aiMovement = GetComponent<PlayerAIMovement>();
  23. playerController = GetComponent<PlayerController>();
  24. }
  25. void Start()
  26. {
  27. rb = GetComponent<Rigidbody>();
  28. // Configure Rigidbody for hockey physics
  29. if (rb != null)
  30. {
  31. rb.constraints = RigidbodyConstraints.FreezeRotationX |
  32. RigidbodyConstraints.FreezeRotationZ |
  33. RigidbodyConstraints.FreezePositionY;
  34. rb.linearDamping = 1f;
  35. }
  36. // Enable/disable AI based on control mode
  37. if (aiMovement != null)
  38. {
  39. aiMovement.IsAIControlled = !isPlayerControlled;
  40. }
  41. // Disable player input if AI controlled
  42. if (playerInput != null)
  43. {
  44. playerInput.enabled = isPlayerControlled;
  45. }
  46. }
  47. public void OnMove(InputAction.CallbackContext context)
  48. {
  49. if (!isPlayerControlled) return;
  50. moveInput = context.ReadValue<Vector2>();
  51. }
  52. void FixedUpdate()
  53. {
  54. if (isPlayerControlled)
  55. {
  56. HandlePlayerMovement();
  57. }
  58. // AI movement is handled by PlayerAIMovement component
  59. }
  60. void HandlePlayerMovement()
  61. {
  62. if (rb == null) return;
  63. // Don't allow movement if frozen (during faceoff)
  64. if (playerController != null && playerController.IsFrozen())
  65. {
  66. // Immediately stop all movement when frozen
  67. rb.linearVelocity = Vector3.zero;
  68. rb.angularVelocity = Vector3.zero;
  69. return;
  70. }
  71. // Don't allow movement if puck is not in play
  72. if (GameManager.Instance != null && !GameManager.Instance.IsPuckInPlay)
  73. {
  74. // Apply deceleration when puck is not in play
  75. if (rb.linearVelocity.magnitude > 0.1f)
  76. {
  77. Vector3 decelerationForce = -rb.linearVelocity.normalized * deceleration;
  78. rb.AddForce(decelerationForce, ForceMode.Acceleration);
  79. }
  80. else
  81. {
  82. rb.linearVelocity = Vector3.zero;
  83. }
  84. return;
  85. }
  86. if (moveInput.magnitude > 0.1f)
  87. {
  88. // Use force-based movement for realistic hockey physics
  89. Vector3 inputDirection = new Vector3(moveInput.x, 0, moveInput.y).normalized;
  90. Vector3 desiredVelocity = inputDirection * moveSpeed;
  91. Vector3 velocityDifference = desiredVelocity - rb.linearVelocity;
  92. // Apply acceleration force
  93. rb.AddForce(velocityDifference * acceleration, ForceMode.Acceleration);
  94. // Clamp velocity to max speed
  95. if (rb.linearVelocity.magnitude > maxSpeed)
  96. {
  97. rb.linearVelocity = rb.linearVelocity.normalized * maxSpeed;
  98. }
  99. // Rotate toward movement direction
  100. Vector3 lookDirection = new Vector3(moveInput.x, 0, moveInput.y);
  101. if (lookDirection != Vector3.zero)
  102. {
  103. Quaternion targetRotation = Quaternion.LookRotation(lookDirection);
  104. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
  105. }
  106. }
  107. else
  108. {
  109. // Apply deceleration when no input
  110. if (rb.linearVelocity.magnitude > 0.1f)
  111. {
  112. Vector3 decelerationForce = -rb.linearVelocity.normalized * deceleration;
  113. rb.AddForce(decelerationForce, ForceMode.Acceleration);
  114. }
  115. else
  116. {
  117. rb.linearVelocity = Vector3.zero;
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// Switch between player and AI control
  123. /// </summary>
  124. public void SetControlMode(bool playerControlled)
  125. {
  126. isPlayerControlled = playerControlled;
  127. if (aiMovement != null)
  128. {
  129. aiMovement.IsAIControlled = !playerControlled;
  130. }
  131. if (playerInput != null)
  132. {
  133. playerInput.enabled = playerControlled;
  134. }
  135. }
  136. }