85 lines
2.7 KiB
C#
85 lines
2.7 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.InputSystem;
|
||
|
|
|
||
|
|
[RequireComponent(typeof(CharacterController))]
|
||
|
|
public class PlayerController : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Header("Movement Settings")]
|
||
|
|
[SerializeField] private float walkSpeed = 5.0f;
|
||
|
|
[SerializeField] private float sprintSpeed = 8.5f;
|
||
|
|
[SerializeField] private float gravity = -19.62f;
|
||
|
|
[SerializeField] private float jumpHeight = 1.2f;
|
||
|
|
|
||
|
|
[Header("Look Settings")]
|
||
|
|
[SerializeField] private Transform cameraHolder;
|
||
|
|
[SerializeField] private float mouseSensitivity = 2.0f;
|
||
|
|
[SerializeField] private float upDownLookLimit = 80.0f;
|
||
|
|
|
||
|
|
private CharacterController characterController;
|
||
|
|
private Vector3 velocity;
|
||
|
|
private Vector2 moveInput;
|
||
|
|
private Vector2 lookInput;
|
||
|
|
private float verticalRotation = 0f;
|
||
|
|
private bool isSprinting;
|
||
|
|
private bool jumpRequested;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
characterController = GetComponent<CharacterController>();
|
||
|
|
Cursor.lockState = CursorLockMode.Locked;
|
||
|
|
Cursor.visible = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
HandleRotation();
|
||
|
|
HandleMovement();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnMove(InputValue value) => moveInput = value.Get<Vector2>();
|
||
|
|
public void OnLook(InputValue value) => lookInput = value.Get<Vector2>();
|
||
|
|
public void OnSprint(InputValue value) => isSprinting = value.isPressed;
|
||
|
|
public void OnJump(InputValue value)
|
||
|
|
{
|
||
|
|
if (value.isPressed && characterController.isGrounded)
|
||
|
|
jumpRequested = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void HandleRotation()
|
||
|
|
{
|
||
|
|
if (cameraHolder == null) return;
|
||
|
|
|
||
|
|
float mouseX = lookInput.x * mouseSensitivity * 0.1f;
|
||
|
|
float mouseY = lookInput.y * mouseSensitivity * 0.1f;
|
||
|
|
|
||
|
|
transform.Rotate(Vector3.up * mouseX);
|
||
|
|
|
||
|
|
verticalRotation -= mouseY;
|
||
|
|
verticalRotation = Mathf.Clamp(verticalRotation, -upDownLookLimit, upDownLookLimit);
|
||
|
|
cameraHolder.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void HandleMovement()
|
||
|
|
{
|
||
|
|
if (characterController.isGrounded && velocity.y < 0)
|
||
|
|
{
|
||
|
|
velocity.y = -2f;
|
||
|
|
}
|
||
|
|
|
||
|
|
float currentSpeed = isSprinting ? sprintSpeed : walkSpeed;
|
||
|
|
Vector3 moveDirection = transform.right * moveInput.x + transform.forward * moveInput.y;
|
||
|
|
characterController.Move(moveDirection * currentSpeed * Time.deltaTime);
|
||
|
|
|
||
|
|
if (jumpRequested)
|
||
|
|
{
|
||
|
|
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||
|
|
jumpRequested = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
velocity.y += gravity * Time.deltaTime;
|
||
|
|
characterController.Move(velocity * Time.deltaTime);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SetCameraHolder(Transform camHolder) => cameraHolder = camHolder;
|
||
|
|
}
|