一、第一人称视角
public class CameraRot : MonoBehaviour { float x; float y; void Update () { x+= Input.GetAxis ("Mouse Y"); y+= Input.GetAxis ("Mouse X");
//两个四元数相乘得到一个最终变化的四元数。 transform.rotation = Quaternion.AngleAxis(y, Vector3.up) * Quaternion.AngleAxis(x, Vector3.left); } }
二、第三人称视角
public class CameraRot : MonoBehaviour { float x; float y; public Transform target; Vector3 dir; void Start(){ dir = target.position - transform.position; } void Update () { x+= Input.GetAxis ("Mouse Y"); y+= Input.GetAxis ("Mouse X");
//四元数乘向量得到变化后的向量 transform.position=target.position-Quaternion.AngleAxis (y, Vector3.up) * Quaternion.AngleAxis(x, Vector3.left)*dir; transform.LookAt (target.position); } }