Всем привет проблема хочу сделать свободную камеру вокруг объекта(автомобиля) при нажатии правой кнопки мыши
в настроящий момент при нажатии правой кнопки мыши камера немного отклоняется от задуманной траектории но после сразу возвращается на исходную, уже пробовал решать через чат гпт все четно, помогите. Следующий код:
using UnityEngine;
public class CarCam : MonoBehaviour
{
Transform rootNode;
Transform carCam;
Transform car;
Rigidbody carPhysics;
private bool isRightMouseButtonDown = false;
private bool isReturning = false;
private float returnTimer = 0f;
private float returnDelay = 10f; // Время до возвращения камеры в исходное положение
[Tooltip("If car speed is below this value, then the camera will default to looking forwards.")]
public float rotationThreshold = 1f;
[Tooltip("How closely the camera follows the car's position. The lower the value, the more the camera will lag behind.")]
public float cameraStickiness = 10.0f;
[Tooltip("How closely the camera matches the car's velocity vector. The lower the value, the smoother the camera rotations, but too much results in not being able to see where you're going.")]
public float cameraRotationSpeed = 5.0f;
void Awake()
{
carCam = Camera.main.GetComponent<Transform>();
rootNode = GetComponent<Transform>();
car = rootNode.parent.GetComponent<Transform>();
carPhysics = car.GetComponent<Rigidbody>();
}
void Update()
{
// Проверяем, нажата ли правая кнопка мыши
if (Input.GetMouseButtonDown(1))
{
isRightMouseButtonDown = true;
}
else if (Input.GetMouseButtonUp(1))
{
isRightMouseButtonDown = false;
}
// Проверяем, нужно ли вернуть камеру в исходное положение
if (isReturning)
{
returnTimer += Time.deltaTime;
if (returnTimer >= returnDelay)
{
isReturning = false;
returnTimer = 0f;
}
}
}
void Start()
{
// Detach the camera so that it can move freely on its own.
rootNode.parent = null;
}
void FixedUpdate()
{
Quaternion look;
// Если камера не возвращается в исходное положение
if (!isReturning)
{
// Moves the camera to match the car's position.
rootNode.position = Vector3.Lerp(rootNode.position, car.position, cameraStickiness * Time.fixedDeltaTime);
// If the car isn't moving, default to looking forwards. Prevents camera from freaking out with a zero velocity getting put into a Quaternion.LookRotation
if (carPhysics.velocity.magnitude < rotationThreshold)
look = Quaternion.LookRotation(car.forward);
else
look = Quaternion.LookRotation(carPhysics.velocity.normalized);
// Rotate the camera towards the velocity vector.
look = Quaternion.Slerp(rootNode.rotation, look, cameraRotationSpeed * Time.fixedDeltaTime);
rootNode.rotation = look;
if (isRightMouseButtonDown)
{
// Обрабатываем вращение камеры
RotateCamera();
}
}
}
void RotateCamera()
{
// Получаем ввод с осей мыши для вращения камеры
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
float rotationX = mouseY * cameraRotationSpeed;
float rotationY = mouseX * cameraRotationSpeed;
// Поворачиваем камеру вокруг машины
rootNode.Rotate(Vector3.right, rotationX);
rootNode.Rotate(Vector3.up, rotationY);
// Сбрасываем таймер возврата камеры
returnTimer = 0f;
}
// Метод для запуска возврата камеры в исходное положение
public void StartReturnToDefaultPosition()
{
isReturning = true;
}
}