Не настраивается поворот камеры в Unity
Вот скрипт для поворота камеры мышкой
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("Camera-Control/MouseLook")]
public class MouseLook : MonoBehaviour
{
public enum RotationAxes { MouseXandY = 0, MouseX = 1, MouseY = 2 };
public RotationAxes axes = RotationAxes.MouseXandY;
public float SpeedCameraX = 2F;
public float SpeedCameraY = 2F;
public float MaxRotateX = -360F;
public float MinRotateX = 360F;
public float MaxRotateY = -360F;
public float MinRotateY = 360F;
float defaultrotateX = 0F;
float defaultrotateY = 0F;
Quaternion originalRotation;
void Start()
{
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
originalRotation = transform.localRotation;
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360) angle += 360F;
if (angle > 360) angle -= -360F;
return Mathf.Clamp(angle, min, max);
}
void Update()
{
if (axes == RotationAxes.MouseXandY)
{
defaultrotateX += Input.GetAxis("Mouse X") * SpeedCameraX;
defaultrotateY += Input.GetAxis("Mouse Y") * SpeedCameraY;
defaultrotateX = ClampAngle(defaultrotateX, MinRotateX, MaxRotateX);
defaultrotateY = ClampAngle(defaultrotateY, MinRotateY, MaxRotateY);
Quaternion xQuaternion = Quaternion.AngleAxis(defaultrotateX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(defaultrotateY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
defaultrotateX += Input.GetAxis("Mouse X") * SpeedCameraX;
defaultrotateX = ClampAngle(defaultrotateX, MinRotateX, MaxRotateX);
Quaternion xQuaternion = Quaternion.AngleAxis(defaultrotateX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else if (axes == RotationAxes.MouseY)
{
defaultrotateY += Input.GetAxis("Mouse Y") * SpeedCameraY;
defaultrotateY = ClampAngle(defaultrotateY, MinRotateY, MaxRotateY);
Quaternion yQuaternion = Quaternion.AngleAxis(defaultrotateY, -Vector3.right);
transform.localRotation = originalRotation * yQuaternion;
}
}
}
Я добавляю его на камеру и на обьект, но при запуске управление мышью не осуществяется. Что не так? Я делал все основываясь на этом видео. Может, у меня какие-то отличия от него? (кроме названий переменных)
Ответы (1 шт):
Автор решения: Yaroslav
→ Ссылка
Код лютое гавно. Не смотрите этот канал, автор даже не подозревает о существовании структур Vector2, Vector3 и углов Эйлера. Unity видит впервые в жизни. Плохому научат.
[DisallowMultipleComponent]
[RequireComponent(typeof(Camera))]
public class CameraMouseController : MonoBehaviour
{
[SerializeField] private Vector2 _speed = new Vector2(9, 16);
[SerializeField] private Vector2 _veritcalLimit = new Vector2(-60, 60);
private Transform _transform;
private void Awake ()
{
_transform = transform;
}
private void Update ()
{
UpdateRotation();
}
private void UpdateRotation ()
{
Vector2 InputAxis = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
Vector3 DeltaRotation = new Vector3(-InputAxis.y*_speed.y, InputAxis.x*_speed.x, 0);
Vector3 NewRotation = _transform.localEulerAngles+DeltaRotation;
NewRotation.x = GetClampAngle(NewRotation.x, _veritcalLimit);
_transform.localEulerAngles = NewRotation;
}
private float GetClampAngle (float angle, Vector2 range)
{
angle = GetAngleFormat180(angle);
angle = Mathf.Clamp(angle, _veritcalLimit.x, _veritcalLimit.y);
return angle;
}
/// <summary> return angle in range from -180 to 180 degrees </summary>
private float GetAngleFormat180 (float angle)
{
while (angle > 180f)
angle += -360f;
while (angle < -180f)
angle += 360f;
return angle;
}
}