Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- [Header("Camera Look")]
- public float mouseSensitivity = 100f;
- public Transform playerCamera;
- private float xRotation = 0f;
- [Header("Movement")]
- public float walkSpeed = 5f;
- public float sprintSpeed = 8f;
- public float crouchSpeed = 2.5f;
- public float jumpHeight = 1.5f;
- public float gravity = -9.81f;
- public float fallMultiplier = 2.5f;
- [Header("Crouch")]
- public float crouchHeight = 1f;
- public float standingHeight = 2f;
- public float crouchTransitionSpeed = 10f;
- [Header("Head Bob")]
- public float bobFrequency = 14f;
- public float bobAmplitude = 0.05f;
- [Header("References")]
- public CharacterController controller;
- public Transform groundCheck;
- public LayerMask groundMask;
- Vector3 velocity;
- bool isGrounded;
- float defaultYPos;
- float timer;
- [HideInInspector] public bool canMove = true;
- void Start()
- {
- if(playerCamera != null)
- defaultYPos = playerCamera.localPosition.y;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- void Update()
- {
- HandleMouseLook();
- if (!canMove)
- {
- velocity = Vector3.zero;
- return;
- }
- isGrounded = Physics.CheckSphere(groundCheck.position, 0.4f, groundMask);
- if (isGrounded && velocity.y < 0)
- {
- velocity.y = -2f;
- }
- float x = Input.GetAxis("Horizontal");
- float z = Input.GetAxis("Vertical");
- float currentSpeed = walkSpeed;
- if (Input.GetKey(KeyCode.LeftShift)) currentSpeed = sprintSpeed;
- if (Input.GetKey(KeyCode.LeftControl)) currentSpeed = crouchSpeed;
- Vector3 move = transform.right * x + transform.forward * z;
- controller.Move(move * currentSpeed * Time.deltaTime);
- if (Input.GetButtonDown("Jump") && isGrounded)
- {
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
- }
- if (velocity.y < 0)
- {
- velocity.y += gravity * fallMultiplier * Time.deltaTime;
- }
- else
- {
- velocity.y += gravity * Time.deltaTime;
- }
- controller.Move(velocity * Time.deltaTime);
- HandleCrouch();
- HandleHeadBob(move, currentSpeed);
- }
- void HandleMouseLook()
- {
- float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
- float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
- xRotation -= mouseY;
- xRotation = Mathf.Clamp(xRotation, -90f, 90f);
- playerCamera.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
- transform.Rotate(Vector3.up * mouseX);
- }
- void HandleCrouch()
- {
- bool isCrouching = Input.GetKey(KeyCode.LeftControl);
- float targetHeight = isCrouching ? crouchHeight : standingHeight;
- controller.height = Mathf.Lerp(controller.height, targetHeight, Time.deltaTime * crouchTransitionSpeed);
- }
- void HandleHeadBob(Vector3 move, float currentSpeed)
- {
- if (!isGrounded) return;
- if (Mathf.Abs(move.x) > 0.1f || Mathf.Abs(move.z) > 0.1f)
- {
- timer += Time.deltaTime * (currentSpeed * 1.5f);
- float newY = defaultYPos + Mathf.Sin(timer * bobFrequency) * bobAmplitude;
- playerCamera.localPosition = new Vector3(playerCamera.localPosition.x, newY, playerCamera.localPosition.z);
- }
- else
- {
- timer = 0;
- playerCamera.localPosition = Vector3.Lerp(playerCamera.localPosition, new Vector3(playerCamera.localPosition.x, defaultYPos, playerCamera.localPosition.z), Time.deltaTime * 10f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment