1、基础知识:四元数和欧拉角
四元数转化成欧拉角 Vector3 v3=transform.rotation.eulerAngles;
欧拉角转换成四元数 Quaternion rotation = Quaternion.Euler(v3);
2、鼠标控制物体旋转
private void RotateControl()
{
float mousex = Input.GetAxis(“Mouse X”);
float mousey = -Input.GetAxis(“Mouse Y”);
Quaternion originRotation = cachedCam.transform.rotation;
Vector3 origion = originRotation.eulerAngles;
Vector3 targetRotation = new Vector3(origion.x+ mousey, origion.y+ mousex , 0);
Vector3 vec= Vector3.Lerp(origion, targetRotation,5f);
cachedCam.transform.localRotation=Quaternion.Euler(vec);
}
3、虚拟轴控制移动
private void MoveControl()
{
float horizontal = Input.GetAxis(“Horizontal”);
float vertical = Input.GetAxis(“Vertical”);
Vector3 direction = new Vector3(horizontal, 0, vertical);
if (direction != Vector3.zero)
{
cachedCam.transform.Translate(Vector3.forward * speed * Time.fixedDeltaTime);
}
}