Unity Quaternion的几种用法

教程地址(观看视频需):

https://learn.unity.com/project/c-survival-guide-rotations?language=en&courseId=5cf06bd1edbc2a58d7fc3209

1.初始化物体旋转角度为0

Instantiate(ObjectPrefab, transform.position, Quaternion.identity);

2. 初始化物体带有旋转角度

Instantiate(ObjectPrefab, Vector3.zero, Quaternion.Euler(0, 35, 0));

3.物体始终转向目标物体

Vector3 directionToFace = _sphere.position - transform.position;        //先算出与目标物体之间的位移差
Debug.DrawRay(transform.position, directionToFace, Color.green);   //画出射线,可以很明显地看出与目标物体之间的关系
transform.rotation = Quaternion.LookRotation(directionToFace);        //使用LookRotation让物体朝向目标体

4.物体缓慢转向目标物体

Vector3 directionToFace = _sphere.position - transform.position;      
Debug.DrawRay(transform.position, directionToFace, Color.green);
Quaternion targetRotation = Quaternion.LookRotation(directionToFace);     //先将旋转值保存出来

transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);    //使用Slerp将旋转值以一定时间实现

你可能感兴趣的:(Unity Quaternion的几种用法)