Newer
Older
HierarchicalFSM-Unity3D / Assets / Scripts / Player / PlayerController.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using UnityEngine;
  5. public class PlayerController : MonoBehaviour
  6. {
  7. private Rigidbody rb;
  8. private Vector2 currentVelocity;
  9. private Vector2 targetVelocity;
  10. private Animator animator;
  11. [SerializeField] private bool crouch = false;
  12. [SerializeField] private bool canCrouchCover = false;
  13. [SerializeField] private float rotationSpeed = 5f;
  14. [SerializeField] private float movementSmoothing = 0.1f; // Adjust this value to control smoothing
  15. public float smoothing = 0.1f; // Adjust this value to control smoothing
  16. private float currentTurnValue;
  17. private float turnVelocity;
  18. private void Awake()
  19. {
  20. rb = GetComponent<Rigidbody>();
  21. animator = GetComponent<Animator>();
  22. }
  23. // Start is called before the first frame update
  24. void Start()
  25. {
  26. animator.applyRootMotion = true;
  27. }
  28. // Update is called once per frame
  29. void Update()
  30. {
  31. IsCrouched();
  32. ApplyRotation();
  33. // Read input values once per frame
  34. float horizontal = Input.GetAxis("Horizontal");
  35. float vertical = Input.GetAxis("Vertical");
  36. // Determine velocity based on whether the player is running
  37. if (IsRunning())
  38. {
  39. //Apply smothness movement
  40. targetVelocity = new Vector2(horizontal, vertical) * 2f;
  41. }
  42. else
  43. {
  44. //Apply smothness movement
  45. targetVelocity = new Vector2(horizontal, vertical);
  46. }
  47. // Smoothly interpolate the current velocity towards the target velocity
  48. currentVelocity = Vector2.Lerp(currentVelocity, targetVelocity, movementSmoothing);
  49. // Update animator parameters
  50. animator.SetFloat("Horizontal", currentVelocity.x);
  51. animator.SetFloat("Vertical", currentVelocity.y);
  52. // Debug logs
  53. //Debug.Log("IsRunning: " + IsRunning());
  54. //Debug.Log("Horizontal: " + velocity.x);
  55. //Debug.Log("Vertical: " + velocity.y);
  56. animator.SetBool("Crouched", crouch);
  57. animator.SetBool("CrouchCover", canCrouchCover);
  58. }
  59. //Checks if the player is running
  60. private bool IsRunning() => Input.GetKey(KeyCode.LeftShift) ? true : false;
  61. private void IsCrouched()
  62. {
  63. if (Input.GetKeyDown(KeyCode.LeftControl))
  64. {
  65. crouch = !crouch;
  66. }
  67. }
  68. void OnCollisionStay(Collision collision)
  69. {
  70. if (Input.GetKeyDown(KeyCode.Q))
  71. {
  72. Debug.Log("COLLISION!");
  73. foreach (ContactPoint contact in collision.contacts)
  74. {
  75. if (contact.otherCollider.tag == "SmallWall") canCrouchCover = !canCrouchCover;
  76. break;
  77. }
  78. }
  79. }
  80. private void OnAnimatorMove()
  81. {
  82. Vector3 pos;
  83. if(canCrouchCover)
  84. {
  85. pos = new Vector3(transform.position.x, transform.position.y, animator.rootPosition.z);
  86. transform.position = pos;
  87. }
  88. else
  89. {
  90. transform.position = animator.rootPosition;
  91. transform.rotation = animator.rootRotation;
  92. }
  93. }
  94. void RotateCharacter(float turnValue)
  95. {
  96. // Set the Turn parameter in the animator
  97. animator.SetFloat("Turn", turnValue);
  98. // Apply the rotation to the character
  99. transform.Rotate(Vector3.up, turnValue * rotationSpeed * Time.deltaTime);
  100. }
  101. void ApplyRotation()
  102. {
  103. // Get mouse input
  104. float mouseX = Input.GetAxis("Mouse X");
  105. // Smooth the input
  106. float targetTurnValue = mouseX;
  107. // Smoothly interpolate the turn value
  108. currentTurnValue = Mathf.Lerp(currentTurnValue, targetTurnValue, smoothing);
  109. // Rotate the character based on the smoothed input
  110. if (mouseX != 0)
  111. {
  112. RotateCharacter(currentTurnValue);
  113. }
  114. else
  115. {
  116. // Smoothly reset the Turn parameter to 0 when there is no rotation input
  117. currentTurnValue = Mathf.Lerp(currentTurnValue, 0, smoothing);
  118. animator.SetFloat("Turn", currentTurnValue);
  119. }
  120. }
  121. }