LuckyEcho

Untitled

May 30th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7. //* THIS SCRIPT CONTROLS THE PLAYER MOVEMENT AND LOOKING
  8.  
  9. // PUBLICS
  10. [Header("Objects")]
  11. [SerializeField] private GameObject Camera;
  12. [SerializeField] private GameObject CameraHolder;
  13. [SerializeField] private GameObject Orientation;
  14. [SerializeField] private GameObject GroundCheck;
  15. [SerializeField] private GameObject CameraPosition;
  16.  
  17. [Header("Movement & Cameras")]
  18. [SerializeField] private float MovementSpeed;
  19. [SerializeField] private float SprintSpeed;
  20. [SerializeField] private float CameraSensitivity;
  21. [SerializeField] private float AirDrag = 1;
  22. [SerializeField] private float GroundDrag = 5;
  23. [SerializeField] private float DownwardForce = 1;
  24. //[SerializeField] private float CameraSmoothing = 1f;
  25.  
  26. [Header("Jumping")]
  27. [SerializeField] private LayerMask GroundLayer;
  28. [SerializeField] private float JumpPower;
  29. [SerializeField] private float GroundCheckRadius = 0.2f;
  30.  
  31. // PRIVATES
  32. private Rigidbody rigidbody;
  33. private Vector2 currentCameraRotation;
  34. private float currentDownwardVelocity = 0f;
  35. [HideInInspector] public bool lookAround = true;
  36.  
  37.  
  38. // Start is called before the first frame update
  39. void Start()
  40. {
  41. // Set up the rigidbody and remove the cursor.
  42. rigidbody = GetComponent<Rigidbody>();
  43. Cursor.lockState = CursorLockMode.Locked;
  44. Cursor.visible = false;
  45.  
  46.  
  47. }
  48.  
  49. // Update is called once per frame
  50. void Update()
  51. {
  52. HandleCamera();
  53. // Check if the jump button is down, and if the player is grounded.
  54. if (InputManager.instance.input.User.Jump.ReadValue<float>() > 0f && InputManager.instance.input.User.Jump.triggered && isGrounded()) {
  55.  
  56. rigidbody.AddForce(Vector3.up * JumpPower, ForceMode.Impulse);
  57. }
  58.  
  59. // Add drag by checking if we are on the ground or not.
  60. if (isGrounded())
  61. {
  62. rigidbody.drag = GroundDrag;
  63. currentDownwardVelocity = 0f;
  64. }
  65. else
  66. {
  67. rigidbody.drag = AirDrag;
  68. currentDownwardVelocity += DownwardForce * Time.deltaTime;
  69. rigidbody.AddForce(Vector3.down * currentDownwardVelocity * Time.deltaTime, ForceMode.Force);
  70. }
  71.  
  72. }
  73.  
  74. // Update is called once per frame for physics.
  75. void FixedUpdate()
  76. {
  77. HandleMovement();
  78. }
  79.  
  80.  
  81.  
  82.  
  83. /// <summary>
  84. /// Handles the camera, call in LateUpdate.
  85. /// </summary>
  86. void HandleCamera()
  87. {
  88. if (lookAround)
  89. {
  90. // Get the mouse inputs and add them to current rotation vector.
  91. currentCameraRotation.x -= InputManager.instance.input.User.Look.ReadValue<Vector2>().y * CameraSensitivity;
  92. currentCameraRotation.y += InputManager.instance.input.User.Look.ReadValue<Vector2>().x * CameraSensitivity;
  93. currentCameraRotation.x = Mathf.Clamp(currentCameraRotation.x, -85, 85);
  94.  
  95. // Set the camera position to the camera position gameobject.
  96. CameraHolder.transform.position = CameraPosition.transform.position;
  97. }
  98.  
  99. // Orientation object is set to the correct rotation, then the players rotation is set to it to prevent player model stuttering, but when setting the camera rotation it doesn't work.
  100. Orientation.transform.rotation = Quaternion.Euler(0, currentCameraRotation.y, 0);
  101. transform.rotation = Orientation.transform.rotation;
  102. CameraHolder.transform.rotation = Quaternion.Euler(currentCameraRotation.x, currentCameraRotation.y, 0);
  103.  
  104. }
  105.  
  106. /// <summary>
  107. /// Handles the movement, call in Update.
  108. /// </summary>
  109. void HandleMovement()
  110. {
  111. // Get the movement directions and multiply them by time.deltatime;
  112. float Horizontal = InputManager.instance.input.User.Movement.ReadValue<Vector2>().x * Time.deltaTime;
  113. float Vertical = InputManager.instance.input.User.Movement.ReadValue<Vector2>().y * Time.deltaTime;
  114.  
  115. // Check if user is sprinting.
  116. float SpeedMultiplier = Input.GetKey(KeyCode.LeftShift) ? SprintSpeed : MovementSpeed;
  117.  
  118. // Apply the force.
  119. rigidbody.AddForce(((Orientation.transform.forward * Vertical + Orientation.transform.right * Horizontal).normalized) * SpeedMultiplier);
  120. }
  121.  
  122. /// <summary>
  123. /// Returns a bool, if the player is grounded on the ground layer.
  124. /// </summary>
  125. /// <returns></returns>
  126. bool isGrounded()
  127. {
  128. return Physics.CheckSphere(GroundCheck.transform.position, GroundCheckRadius, GroundLayer);
  129. }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment