Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- //* THIS SCRIPT CONTROLS THE PLAYER MOVEMENT AND LOOKING
- // PUBLICS
- [Header("Objects")]
- [SerializeField] private GameObject Camera;
- [SerializeField] private GameObject CameraHolder;
- [SerializeField] private GameObject Orientation;
- [SerializeField] private GameObject GroundCheck;
- [SerializeField] private GameObject CameraPosition;
- [Header("Movement & Cameras")]
- [SerializeField] private float MovementSpeed;
- [SerializeField] private float SprintSpeed;
- [SerializeField] private float CameraSensitivity;
- [SerializeField] private float AirDrag = 1;
- [SerializeField] private float GroundDrag = 5;
- [SerializeField] private float DownwardForce = 1;
- //[SerializeField] private float CameraSmoothing = 1f;
- [Header("Jumping")]
- [SerializeField] private LayerMask GroundLayer;
- [SerializeField] private float JumpPower;
- [SerializeField] private float GroundCheckRadius = 0.2f;
- // PRIVATES
- private Rigidbody rigidbody;
- private Vector2 currentCameraRotation;
- private float currentDownwardVelocity = 0f;
- [HideInInspector] public bool lookAround = true;
- // Start is called before the first frame update
- void Start()
- {
- // Set up the rigidbody and remove the cursor.
- rigidbody = GetComponent<Rigidbody>();
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- // Update is called once per frame
- void Update()
- {
- HandleCamera();
- // Check if the jump button is down, and if the player is grounded.
- if (InputManager.instance.input.User.Jump.ReadValue<float>() > 0f && InputManager.instance.input.User.Jump.triggered && isGrounded()) {
- rigidbody.AddForce(Vector3.up * JumpPower, ForceMode.Impulse);
- }
- // Add drag by checking if we are on the ground or not.
- if (isGrounded())
- {
- rigidbody.drag = GroundDrag;
- currentDownwardVelocity = 0f;
- }
- else
- {
- rigidbody.drag = AirDrag;
- currentDownwardVelocity += DownwardForce * Time.deltaTime;
- rigidbody.AddForce(Vector3.down * currentDownwardVelocity * Time.deltaTime, ForceMode.Force);
- }
- }
- // Update is called once per frame for physics.
- void FixedUpdate()
- {
- HandleMovement();
- }
- /// <summary>
- /// Handles the camera, call in LateUpdate.
- /// </summary>
- void HandleCamera()
- {
- if (lookAround)
- {
- // Get the mouse inputs and add them to current rotation vector.
- currentCameraRotation.x -= InputManager.instance.input.User.Look.ReadValue<Vector2>().y * CameraSensitivity;
- currentCameraRotation.y += InputManager.instance.input.User.Look.ReadValue<Vector2>().x * CameraSensitivity;
- currentCameraRotation.x = Mathf.Clamp(currentCameraRotation.x, -85, 85);
- // Set the camera position to the camera position gameobject.
- CameraHolder.transform.position = CameraPosition.transform.position;
- }
- // 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.
- Orientation.transform.rotation = Quaternion.Euler(0, currentCameraRotation.y, 0);
- transform.rotation = Orientation.transform.rotation;
- CameraHolder.transform.rotation = Quaternion.Euler(currentCameraRotation.x, currentCameraRotation.y, 0);
- }
- /// <summary>
- /// Handles the movement, call in Update.
- /// </summary>
- void HandleMovement()
- {
- // Get the movement directions and multiply them by time.deltatime;
- float Horizontal = InputManager.instance.input.User.Movement.ReadValue<Vector2>().x * Time.deltaTime;
- float Vertical = InputManager.instance.input.User.Movement.ReadValue<Vector2>().y * Time.deltaTime;
- // Check if user is sprinting.
- float SpeedMultiplier = Input.GetKey(KeyCode.LeftShift) ? SprintSpeed : MovementSpeed;
- // Apply the force.
- rigidbody.AddForce(((Orientation.transform.forward * Vertical + Orientation.transform.right * Horizontal).normalized) * SpeedMultiplier);
- }
- /// <summary>
- /// Returns a bool, if the player is grounded on the ground layer.
- /// </summary>
- /// <returns></returns>
- bool isGrounded()
- {
- return Physics.CheckSphere(GroundCheck.transform.position, GroundCheckRadius, GroundLayer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment