Unity_1lab/1 laba/Assets/Scripts/PlayerController.cs
2025-09-15 19:16:35 +03:00

132 lines
3.7 KiB
C#

using UnityEngine;
public class PlaerController : MonoBehaviour
{
[SerializeField] private CharacterController characterController;
[SerializeField] private Transform cameraTransform;
[SerializeField] private Transform checkGroundTransform;
[SerializeField] private LayerMask groundMask;
[Header("Settings")]
[SerializeField] private float checkRadiusSphere = 0.2f;
[SerializeField] private float gravity = -10f;
[SerializeField] private float speed = 4f;
[SerializeField] private float speedRun = 7f;
[SerializeField] private float jumpHeight = 1f;
[SerializeField] public float stamina = 100f;
[SerializeField] private float staminaMinus = 15f;
[SerializeField] private float staminaPlus = 15f;
[SerializeField] private float timeToRegen = 1f;
[Range(1f, 2000f)]
[SerializeField] private float sensetivity = 200f;
private HUD hud;
private float timerToRegen = 5f;
private float rotationX;
private bool isGround;
private Vector3 velocity;
private Vector3 move;
private void Awake()
{
hud = GameObject.FindGameObjectWithTag("Canvas").GetComponent<HUD>();
}
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Debug.Log($"PlaerController: Начальная сенса = {sensetivity}");
}
void Update()
{
Velocity();
Move();
Rotate();
}
private void Rotate()
{
float mouseX = Input.GetAxis("Mouse X") * sensetivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensetivity * Time.deltaTime;
rotationX -= mouseY;
rotationX = Mathf.Clamp(rotationX, -90f, 90f);
cameraTransform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.Rotate(0, mouseX, 0);
}
public float GetStamina()
{
return stamina;
}
private void Move()
{
float moveX = Input.GetAxis("Horizontal");
float moveY = Input.GetAxis("Vertical");
move = transform.forward * moveY + transform.right * moveX;
if (Input.GetKey(KeyCode.LeftShift) && (moveX != 0 || moveY != 0) && stamina > 0)
{
characterController.Move(move * speedRun * Time.deltaTime);
stamina -= staminaMinus * Time.deltaTime;
hud.staminaBar.fillAmount = stamina / 100;
stamina = Mathf.Clamp(stamina, 0, 100);
timerToRegen = 0f;
}
else
{
characterController.Move(move * speed * Time.deltaTime);
if (timerToRegen < timeToRegen)
{
timerToRegen += Time.deltaTime;
}
else if (timerToRegen >= timeToRegen && stamina < 100f)
{
Regeneration();
}
}
}
private void Regeneration()
{
stamina += staminaPlus * Time.deltaTime;
hud.staminaBar.fillAmount = stamina / 100;
stamina = Mathf.Clamp(stamina, 0, 100);
}
private void Velocity()
{
isGround = Physics.CheckSphere(checkGroundTransform.position, checkRadiusSphere, groundMask);
if (isGround && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetButtonDown("Jump") && isGround)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += Time.deltaTime * gravity;
characterController.Move(velocity * Time.deltaTime);
}
public void SetSensitivity(float value)
{
sensetivity = Mathf.Clamp(value, 1f, 2000f);
Debug.Log($"PlaerController: Установлена сенса = {sensetivity}");
}
public float GetSensitivity()
{
return sensetivity;
}
}