unity 由两个向量求夹角

1.

float angle = Vector3.Angle (fromVector, toVector); //求出两向量之间的夹角  
Vector3 normal = Vector3.Cross (fromVector,toVector);//叉乘求出法线向量  
angle *= Mathf.Sign (Vector3.Dot(normal,upVector));  //求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向  

2.

Vector3 velocity = Quaternion.Inverse (transform.rotation)*destinationVector; //对目标向量进行反向旋转,得到的新向量与z轴的夹角即为目标向量与当前物体方向的夹角          
float angle = Mathf.Atan2 (velocity.x,velocity.z) * Mathf.Rad2Deg; //返回tan值为x/z的角的弧度,再转化为度数。  

3.

数学法:已知a,b两个向量

cosθ=X;  (X=(a*b)/(|a|*|b|))

然后求θ=arccosX c#里是Mathf.Acos(X);

posted @ 2017-12-03 21:06  游戏鼻祖  阅读(19784)  评论(0编辑  收藏  举报