Добавил вторую машину и возможность садиться в него
This commit is contained in:
1 parent
a33b4cd5f5
commit
1c106ea853
148 files changed
+26580
-3582
No files matched your search
@@ -3,57 +3,52 @@ using UnityEngine;
|
||||
public class CarOrbitCamera : MonoBehaviour
|
||||
{
|
||||
[Header("Цель")]
|
||||
[SerializeField] private Transform target; // Корневой объект машины
|
||||
[SerializeField] private Vector3 targetOffset = new Vector3(0, 1.2f, 0); // Смещение центра (чтобы смотреть на крышу, а не под колеса)
|
||||
private Transform target; // Убрали SerializeField, теперь цель задается через код
|
||||
[SerializeField] private Vector3 targetOffset = new Vector3(0, 1.2f, 0);
|
||||
|
||||
[Header("Настройки орбиты")]
|
||||
[SerializeField] private float distance = 5.5f; // Дистанция от машины
|
||||
[SerializeField] private float mouseSensitivity = 150f; // Чувствительность мыши
|
||||
[SerializeField] private float distance = 5.5f;
|
||||
[SerializeField] private float mouseSensitivity = 150f;
|
||||
|
||||
[Header("Ограничения (чтобы камера не уходила под землю)")]
|
||||
[SerializeField] private float yMinLimit = 0f; // Минимальный угол (0 = уровень земли)
|
||||
[SerializeField] private float yMaxLimit = 80f; // Максимальный угол (вид сверху)
|
||||
[Header("Ограничения")]
|
||||
[SerializeField] private float yMinLimit = 0f;
|
||||
[SerializeField] private float yMaxLimit = 80f;
|
||||
|
||||
private float currentX = 0f; // Поворот по горизонтали
|
||||
private float currentY = 20f; // Поворот по вертикали (начинаем с легкого наклона)
|
||||
private float currentX = 0f;
|
||||
private float currentY = 20f;
|
||||
|
||||
// НОВЫЙ МЕТОД: Принимает новую машину и сразу сбрасывает углы
|
||||
public void SetTarget(Transform newTarget)
|
||||
{
|
||||
target = newTarget;
|
||||
ResetCameraAngles();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// При старте (или посадке) задаем начальный угол, чтобы камера смотрела в ту же сторону, куда и капот
|
||||
if (target != null)
|
||||
{
|
||||
currentX = target.eulerAngles.y;
|
||||
}
|
||||
}
|
||||
|
||||
// Вызывается после того, как все объекты (включая машину) уже сдвинулись
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (target == null) return;
|
||||
if (target == null) return; // Если цели нет (игрок пешком), камера ничего не делает
|
||||
|
||||
// 1. Читаем движение мыши
|
||||
currentX += Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
|
||||
currentY -= Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
|
||||
|
||||
// 2. Ограничиваем угол по вертикали, чтобы камера не перекрутилась через крышу или не ушла в асфальт
|
||||
currentY = Mathf.Clamp(currentY, yMinLimit, yMaxLimit);
|
||||
|
||||
// 3. Вычисляем новое вращение камеры на основе углов
|
||||
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
|
||||
|
||||
// 4. Вычисляем точку, вокруг которой крутимся (центр машины + смещение вверх)
|
||||
Vector3 focusPoint = target.position + targetOffset;
|
||||
|
||||
// 5. Вычисляем финальную позицию камеры: отходим назад от фокусной точки на расстояние distance
|
||||
Vector3 position = focusPoint - (rotation * Vector3.forward * distance);
|
||||
|
||||
// 6. Применяем позицию и вращение
|
||||
transform.position = position;
|
||||
transform.rotation = rotation;
|
||||
}
|
||||
|
||||
// Метод для вызова из скрипта VehicleInteraction (опционально)
|
||||
// Чтобы камера всегда сбрасывалась ровно за машину, когда игрок садится в неё
|
||||
public void ResetCameraAngles()
|
||||
{
|
||||
if (target != null)
|
||||
|
||||
Reference in new issue
Block a user