public static Quaternion Euler(Vector3 euler);
传入一个向量,使物体旋转,如:
cube.transform.rotation=Quaternion.Euler(new Vector3(0,30,0));
值得注意的是:Unity中组件Transform的Rotation实际上是Quaternion类型的
public static Quaternion Euler(float x, float y, float z);
与上面功能类似,但更为简洁,如:
cube.transform.rotation=Quaternion.Euler(0,30,0);
public static Quaternion Lerp(Quaternion a, Quaternion b, float t);
线性插值,与Mathf.Lerp差不多。
public static Quaternion LookRotation(Vector3 forward, [DefaultValue("Vector3.up")] Vector3 upwards);
与transform.LookAt类似的是都是将其正方向指向目标方向,但是LookRotation可以做的跟更好
如:
public class QuaternionTest : MonoBehaviour
{
public GameObject cube;
public GameObject target;
// Start is called before the first frame update
void Start()
{
Quaternion q= Quaternion.LookRotation(target.transform.position-cube.transform.position);
cube.transform.rotation=Quaternion.Lerp(cube.transform.rotation,q,0.05f);
}
}
值得注意的是:传入参数Vector3 forward是一个向量
public static Quaternion AngleAxis(float angle, Vector3 axis);
如:
public class QuaternionTest : MonoBehaviour
{
public GameObject cube;
public GameObject target;
// Start is called before the first frame update
private void Start() {
// var q1=Quaternion.Euler(0,30,0);
// var q2=Quaternion.Euler(0,60,0);
// var q3=q1*q2;
//cube旋转90°
// cube.transform.rotation=q3;
}
void Update()
{
//cube.transform.rotation=Quaternion.Euler(0,30,0);
//cube.transform.rotation=Quaternion.Euler(0,30,0);
// Quaternion q= Quaternion.LookRotation(target.transform.position-cube.transform.position);
// cube.transform.rotation=Quaternion.Lerp(cube.transform.rotation,q,0.05f);
var mouseX=Input.GetAxis("Mouse X");
Quaternion q=Quaternion.AngleAxis(mouseX,Vector3.up);
cube.transform.rotation*=q;
}
}
值得注意的是:两个四元数相乘,是在原始的角度上,再旋转相应的度数
Vector3 eulerAngles=cube.transform.rotation.eulerAngles;
Debug.Log(eulerAngles);
Unity把四元数以欧拉角的形式展示在Inspector面板上文章来源:https://www.toymoban.com/news/detail-615959.html
四元数与向量相乘:返回值为向量,相当于把向量旋转了一个角度。文章来源地址https://www.toymoban.com/news/detail-615959.html
到了这里,关于四元数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!