48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
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();
|
||
|
||
yaw += mouseDelta.x * mouseSensitivity * Time.deltaTime;
|
||
pitch -= mouseDelta.y * mouseSensitivity * 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;
|
||
}
|
||
} |