涉及知识点:

1.欧拉角、四元数的转换

2.三维空间中的点、向量的关系

3.运用以下函数:

Quaternion.Euler()

Vector3 Vc=Point1(vc3)-Point2(vc3)

Vector3.Lerp

Vector3.Slerp

Quaternion.lerp

 

  currentDistance = target.position - transform.position;//应该用目标物体坐标减去运动物体 
//currentDistance = transform.position - target.position;
  

 

 

var tempQuaternion = Quaternion.Euler(xAngel, yAngle, zAngle);

  xAngel -= Input.GetAxis("Mouse X");
  yAngle += Input.GetAxis("Mouse Y");

 

var move = tempQuaternion * currentDistance ;//四元数旋转作用于(静止模型指向运动摄像机的向量)

 

transform.position = target.position + move;//更新位置
transform.rotation = tempQuaternion;//更新旋转

 

注:

1.在Update()中设置物体的transform.position,需知,如果在某行代码中通过改变某个变量的值来实现位置的偏移,那么不应该同时在其他行代码中去尝试改变位置偏移,因为在Update()中的代码,均会在每一帧按照顺序全部执行一遍,假设你在108行想让物体向左偏移2单位,而在138行想让物体向上偏移2行,最终物体是静止不动的。

2.实现平滑移动或者取值的函数,应该放在Update()或者Lateupdate()中循环执行,方能达到效果。