【发布时间】:2019-06-30 17:19:21
【问题描述】:
我试图在 Unity 中移动相机,但我遇到了一个问题,播放器总是从 0,0,0 旋转开始,即使我在播放前将它在 Y 轴上旋转。
我在代码中将播放器在 Y 轴上旋转,相机在 X 轴上旋转,所以玩家在上下看时不会旋转。
代码如下:
public float cameraSpeed;
//public float smooth = 2;
[Range(0f, 1f)] public float smooth = 0.5f;
Vector2 mouseLook;
Vector2 smoothV;
private Transform cameraTransform;
void Start()
{
//Inicializamos componentes
rb = GetComponent<Rigidbody>();
cameraTransform = Camera.main.transform;
mouseLook.y = Mathf.Clamp(-cameraTransform.localRotation.x, -75, 50);
mouseLook.x = transform.localRotation.y;
}
void CameraMovement()
{
//Declaramos un vector con la direccion del raton
Vector2 md = new Vector2(InputManager.main.HorizontalMouse(), InputManager.main.VerticalMouse());
md *= cameraSpeed / smooth;
smoothV = Vector2.Lerp(smoothV, md, smooth);
mouseLook += smoothV;
//Limitamos el angulo de la camara para que no de vueltas
mouseLook = new Vector2(mouseLook.x, Mathf.Clamp(mouseLook.y, -75, 50));
//Hacemos que rote la camara en el eje x y el jugador en el eje y
Camera.main.transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
transform.localRotation = Quaternion.AngleAxis(mouseLook.x , transform.up);
}
【问题讨论】: