1.万向节死锁无法解决,这是因为它的特性就是如此。就像玻璃杯就是玻璃,这不可否认。(别钻牛角尖昂)
2.大多数情况下欧拉角足够用,例如(CF---摄像机不可能绕z轴旋转,x轴旋转也不会超过九十度,因为那样人物的腰子会被扭断。塔防游戏---保卫萝卜。吃鸡---第三人称视角欧拉角足够)。
3.如果是太空类游戏,为了自由感必须对摄像机视角进行无限滑移,即在任意方向上都能够按照正常思维逻辑进行旋转。则欧拉角不适用,应使用四元数或其他方案。
4.四元数解决方案:
最重要的一句:transform.localRotation *= Quaternion.Euler(freeMove_Mouse);
将以下脚本给予摄像机即可。
鼠标控制屏幕旋转,QE控制z轴旋转,WASD控制人物移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Text : MonoBehaviour
{
Vector3 freeMove_Mouse = new Vector3(0, 0, 0); //存放鼠标移动信息
Vector3 freeMove_Position = new Vector3(0, 0, 0); //存放位置移动信息
void Update()
{
//旋转
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
freeMove_Mouse.y += Input.GetAxis("Mouse X");
freeMove_Mouse.x -= Input.GetAxis("Mouse Y");
}
if (Input.GetKey(KeyCode.Q))
{
freeMove_Mouse.z += 0.2f;
}
if (Input.GetKey(KeyCode.E))
{
freeMove_Mouse.z -= 0.2f;
}
transform.localRotation *= Quaternion.Euler(freeMove_Mouse);
freeMove_Mouse = Vector3.zero;
//不重要
if(Input.GetAxis("Vertical")!=0 || Input.GetAxis("Horizontal") != 0)
{
freeMove_Position.z += Input.GetAxis("Vertical");
freeMove_Position.x += Input.GetAxis("Horizontal");
}
transform.Translate(freeMove_Position*Time.deltaTime*5);
freeMove_Position = Vector3.zero;
}
}