54 lines
1.9 KiB
C#
54 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerLook : MonoBehaviour
|
|
{
|
|
[Header("Look Settings")]
|
|
[SerializeField] private float mouseSensitivity = 150f;
|
|
[SerializeField] private Transform playerBody;
|
|
|
|
private float xRotation = 0f;
|
|
|
|
public float Sensitivity => mouseSensitivity;
|
|
|
|
private void Awake()
|
|
{
|
|
if (playerBody == null) playerBody = transform.parent != null ? transform.parent : transform;
|
|
mouseSensitivity = PlayerPrefs.GetFloat("perekup_sensitivity", mouseSensitivity);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
// Блокируем курсор в центре экрана и скрываем его (если не открыто меню)
|
|
GameInputLock.ApplyCursor();
|
|
}
|
|
|
|
public void SetSensitivity(float value)
|
|
{
|
|
mouseSensitivity = Mathf.Clamp(value, 20f, 600f);
|
|
PlayerPrefs.SetFloat("perekup_sensitivity", mouseSensitivity);
|
|
}
|
|
|
|
/// <summary>Поворот взгляда в заданном направлении (после телепорта/выхода из машины).</summary>
|
|
public void ResetPitch(float pitch = 0f)
|
|
{
|
|
xRotation = pitch;
|
|
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
|
}
|
|
|
|
public void ProcessLook(Vector2 input)
|
|
{
|
|
float mouseX = input.x * mouseSensitivity * Time.deltaTime;
|
|
float mouseY = input.y * mouseSensitivity * Time.deltaTime;
|
|
|
|
// Расчет наклона камеры с ограничением, чтобы нельзя было сломать шею
|
|
xRotation -= mouseY;
|
|
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
|
|
|
|
// Применяем вращение камеры (вверх/вниз)
|
|
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
|
|
|
// Вращаем само тело игрока (влево/вправо)
|
|
playerBody.Rotate(Vector3.up * mouseX);
|
|
}
|
|
}
|