ambrose01

PlayerController.cs

Jan 1st, 2026
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5. [Header("Camera Look")]
  6. public float mouseSensitivity = 100f;
  7. public Transform playerCamera;
  8. private float xRotation = 0f;
  9.  
  10. [Header("Movement")]
  11. public float walkSpeed = 5f;
  12. public float sprintSpeed = 8f;
  13. public float crouchSpeed = 2.5f;
  14. public float jumpHeight = 1.5f;
  15. public float gravity = -9.81f;
  16. public float fallMultiplier = 2.5f;
  17.  
  18. [Header("Crouch")]
  19. public float crouchHeight = 1f;
  20. public float standingHeight = 2f;
  21. public float crouchTransitionSpeed = 10f;
  22.  
  23. [Header("Head Bob")]
  24. public float bobFrequency = 14f;
  25. public float bobAmplitude = 0.05f;
  26.  
  27. [Header("References")]
  28. public CharacterController controller;
  29. public Transform groundCheck;
  30. public LayerMask groundMask;
  31.  
  32. Vector3 velocity;
  33. bool isGrounded;
  34. float defaultYPos;
  35. float timer;
  36.  
  37. [HideInInspector] public bool canMove = true;
  38.  
  39. void Start()
  40. {
  41. if(playerCamera != null)
  42. defaultYPos = playerCamera.localPosition.y;
  43.  
  44. Cursor.lockState = CursorLockMode.Locked;
  45. Cursor.visible = false;
  46. }
  47.  
  48. void Update()
  49. {
  50. HandleMouseLook();
  51.  
  52. if (!canMove)
  53. {
  54. velocity = Vector3.zero;
  55. return;
  56. }
  57.  
  58. isGrounded = Physics.CheckSphere(groundCheck.position, 0.4f, groundMask);
  59. if (isGrounded && velocity.y < 0)
  60. {
  61. velocity.y = -2f;
  62. }
  63.  
  64. float x = Input.GetAxis("Horizontal");
  65. float z = Input.GetAxis("Vertical");
  66.  
  67. float currentSpeed = walkSpeed;
  68. if (Input.GetKey(KeyCode.LeftShift)) currentSpeed = sprintSpeed;
  69. if (Input.GetKey(KeyCode.LeftControl)) currentSpeed = crouchSpeed;
  70.  
  71. Vector3 move = transform.right * x + transform.forward * z;
  72. controller.Move(move * currentSpeed * Time.deltaTime);
  73.  
  74. if (Input.GetButtonDown("Jump") && isGrounded)
  75. {
  76. velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  77. }
  78.  
  79. if (velocity.y < 0)
  80. {
  81. velocity.y += gravity * fallMultiplier * Time.deltaTime;
  82. }
  83. else
  84. {
  85. velocity.y += gravity * Time.deltaTime;
  86. }
  87.  
  88. controller.Move(velocity * Time.deltaTime);
  89.  
  90. HandleCrouch();
  91. HandleHeadBob(move, currentSpeed);
  92. }
  93.  
  94. void HandleMouseLook()
  95. {
  96. float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  97. float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  98.  
  99. xRotation -= mouseY;
  100. xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  101.  
  102. playerCamera.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
  103.  
  104. transform.Rotate(Vector3.up * mouseX);
  105. }
  106.  
  107. void HandleCrouch()
  108. {
  109. bool isCrouching = Input.GetKey(KeyCode.LeftControl);
  110. float targetHeight = isCrouching ? crouchHeight : standingHeight;
  111. controller.height = Mathf.Lerp(controller.height, targetHeight, Time.deltaTime * crouchTransitionSpeed);
  112. }
  113.  
  114. void HandleHeadBob(Vector3 move, float currentSpeed)
  115. {
  116. if (!isGrounded) return;
  117.  
  118. if (Mathf.Abs(move.x) > 0.1f || Mathf.Abs(move.z) > 0.1f)
  119. {
  120. timer += Time.deltaTime * (currentSpeed * 1.5f);
  121. float newY = defaultYPos + Mathf.Sin(timer * bobFrequency) * bobAmplitude;
  122. playerCamera.localPosition = new Vector3(playerCamera.localPosition.x, newY, playerCamera.localPosition.z);
  123. }
  124. else
  125. {
  126. timer = 0;
  127. playerCamera.localPosition = Vector3.Lerp(playerCamera.localPosition, new Vector3(playerCamera.localPosition.x, defaultYPos, playerCamera.localPosition.z), Time.deltaTime * 10f);
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment