Kursach_StealthAction/Assets/Scripts/CameraFollow.cs
2026-10-19 20:14:24 +03:00

49 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
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();
float sens = mouseSensitivity * GameSettings.GetMouseSensitivity();
yaw += mouseDelta.x * sens * Time.deltaTime;
pitch -= mouseDelta.y * sens * Time.deltaTime;
// Ограничиваем угол наклона камеры
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;
}
}