Files
Kursach_StealthAction/Assets/Scripts/CameraFollow.cs
T

49 lines
1.8 KiB
C#
Raw Normal View History

2026-10-19 20:14:24 +03:00
using UnityEngine;
2026-10-06 16:31:45 +03:00
using UnityEngine.InputSystem;
public class CameraFollow : MonoBehaviour
{
[Header("За кем следить")]
public Transform target; // Сюда перетащить игрока (Player)
[Header("Настройки орбиты")]
public float distance = 4.0f; // Дистанция от игрока
public float height = 1.5f; // Высота над игроком
public float mouseSensitivity = 10f; // Чувствительность мыши (в New Input System значения могут отличаться)
private float pitch = 0f; // Вращение вверх/вниз (X)
private float yaw = 0f; // Вращение влево/вправо (Y)
void Start()
{
Vector3 angles = transform.eulerAngles;
pitch = angles.x;
yaw = angles.y;
}
void LateUpdate()
{
if (target == null) return;
var mouse = Mouse.current;
if (mouse != null)
{
// Читаем дельту мыши (смещение курсора за один кадр)
Vector2 mouseDelta = mouse.delta.ReadValue();
2026-10-19 20:14:24 +03:00
float sens = mouseSensitivity * GameSettings.GetMouseSensitivity();
yaw += mouseDelta.x * sens * Time.deltaTime;
pitch -= mouseDelta.y * sens * Time.deltaTime;
2026-10-06 16:31:45 +03:00
// Ограничиваем угол наклона камеры
pitch = Mathf.Clamp(pitch, -40f, 60f);
}
// Вращаем и позиционируем камеру
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0f);
Vector3 position = target.position - (rotation * Vector3.forward * distance) + (Vector3.up * height);
transform.position = position;
transform.rotation = rotation;
}
}