Quaternion.Euler(欧拉角)
Quaternion qt = this.transform.rotation;
Vector3 euler = qt.eulerAngles;
Quaternion qt = Quaternion.AngleAxis( 50 , Vector3.up );
相当于:Quaternion.Euler( 0 , 50, 0);
ps:z轴注视一个方向
1⃣️,没有缓动
this.transform.rotation = Quaternion.LookRotation(targetVec - this.transform.position);
相当于:this.transform.LookAt(targetVec - this.transform.postion);
2⃣️,先快后慢
Quaternion targetQt = Quaternion.LookRotation(targetVec - this.transform.position);
this.transform.rotation = Quaternion.Lerp( this.transform.rotation,targetQt, 0.1f );
3⃣️,匀速
Quaternion targetQt = Quaternion.LookRotation(targetVec - this.transform.position);
this.transform.rotation = Quaternion.RotateTowards( this.transform.rotation,targetQt, 0.1f );
float a = Quaternion.Angle( this.transform.rotation,targetQt );
if( a < = 2 ){
this.transform.rotation = targetQt;
}
1⃣️,right(无缓动)
this.transform.right = targetVec - this.transform.position;
2⃣️,FromToRotation(无缓动)
this.transtorm.rotation = Quaternion.FromToRotation( Vector3.right, targetVec );
例子:
①,分析,如下图所示
a, 首先垂直向下打一条射线
b, 通过此射线能够得到强面的法向量
c,得到法向量, .....代码如下:
void Update() { if (Input.GetMouseButtonDown(0))//监听鼠标左键 { if (Physics.Raycast(this.transform.position, -Vector3.up, out this.hit, 100, this.layerMap))//向正下方发射光线检测 { Quaternion qt = Quaternion.FromToRotation(Vector3.up, this.hit.normal);//以Y轴,法线旋转 this.transform.rotation = qt; } } }
3⃣️, 缓动(套Lerp)