|
|
@@ -1,11 +1,28 @@
|
|
|
using UnityEngine;
|
|
|
+using UnityEngine.Rendering;
|
|
|
|
|
|
-public class PlayerController: MonoBehaviour
|
|
|
+public class PlayerController : MonoBehaviour
|
|
|
{
|
|
|
- public float moveSpeed = 6f;
|
|
|
+ [Header("Movement")]
|
|
|
+ public float walkSpeed = 6f;
|
|
|
+ public float sprintSpeed = 10f;
|
|
|
+ private float currentSpeed;
|
|
|
+
|
|
|
+ [Header("Verticality")]
|
|
|
public float gravity = -9.81f;
|
|
|
public float jumpHeight = 2f;
|
|
|
|
|
|
+ [Header("Camera")]
|
|
|
+ public float mouseSensitivity = 200f;
|
|
|
+ public Transform cameraHolder;
|
|
|
+ private float xRotation = 0f;
|
|
|
+
|
|
|
+ [Header("Stamina")]
|
|
|
+ public int Stamina = 5;
|
|
|
+ public int maxStamina = 5;
|
|
|
+ private float staminaTimer = 0f;
|
|
|
+ private float regenTimer = 0f;
|
|
|
+
|
|
|
private CharacterController controller;
|
|
|
private Vector3 velocity;
|
|
|
private bool isGrounded;
|
|
|
@@ -13,10 +30,52 @@ public class PlayerController: MonoBehaviour
|
|
|
void Start()
|
|
|
{
|
|
|
controller = GetComponent<CharacterController>();
|
|
|
+
|
|
|
+ Cursor.lockState = CursorLockMode.Locked;
|
|
|
}
|
|
|
|
|
|
void Update()
|
|
|
{
|
|
|
+ float x = Input.GetAxis("Horizontal");
|
|
|
+ float y = Input.GetAxis("Vertical");
|
|
|
+ bool isMoving = (x != 0 || y != 0);
|
|
|
+ bool isSprinting = Input.GetKey(KeyCode.LeftShift);
|
|
|
+
|
|
|
+ if (!isMoving && !isSprinting && Stamina < maxStamina)
|
|
|
+ {
|
|
|
+ regenTimer += Time.deltaTime;
|
|
|
+
|
|
|
+ if (regenTimer >= 1f) {
|
|
|
+ Stamina++;
|
|
|
+ regenTimer = 0f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ regenTimer = 0f;
|
|
|
+ }
|
|
|
+
|
|
|
+ Camera();
|
|
|
+ Movement();
|
|
|
+ }
|
|
|
+
|
|
|
+ void Camera()
|
|
|
+ {
|
|
|
+ float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
|
|
|
+ float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
|
|
|
+
|
|
|
+ transform.Rotate(Vector3.up * mouseX);
|
|
|
+
|
|
|
+ xRotation -= mouseY;
|
|
|
+ xRotation = Mathf.Clamp(xRotation, -80f, 80f);
|
|
|
+
|
|
|
+ cameraHolder.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
|
|
+ }
|
|
|
+
|
|
|
+ void Movement()
|
|
|
+ {
|
|
|
+ bool isSprinting = Input.GetKey(KeyCode.LeftShift);
|
|
|
+
|
|
|
isGrounded = controller.isGrounded;
|
|
|
if (isGrounded && velocity.y < 0)
|
|
|
{
|
|
|
@@ -27,7 +86,24 @@ public class PlayerController: MonoBehaviour
|
|
|
float z = Input.GetAxis("Vertical");
|
|
|
Vector3 move = transform.right * x + transform.forward * z;
|
|
|
|
|
|
- controller.Move(move * moveSpeed * Time.deltaTime);
|
|
|
+ if (isSprinting && Stamina > 0)
|
|
|
+ {
|
|
|
+ currentSpeed = sprintSpeed;
|
|
|
+ staminaTimer += Time.deltaTime;
|
|
|
+
|
|
|
+ if (staminaTimer >= 0.5f)
|
|
|
+ {
|
|
|
+ Stamina--;
|
|
|
+ staminaTimer = 0f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ currentSpeed = walkSpeed;
|
|
|
+ staminaTimer = 0f;
|
|
|
+ }
|
|
|
+
|
|
|
+ controller.Move(move * currentSpeed * Time.deltaTime);
|
|
|
|
|
|
if (Input.GetButtonDown("Jump") && isGrounded)
|
|
|
{
|