API explanation and usage of rotation in Unity
There are two main methods of rotation in Unity: Euler angle and quaternion. For a brief introduction of each method, please refer to the summary I wrote before.
I don’t have enough time, and there are a lot of things to read, so if someone else has written it, I will not re-summarize it, and directly borrow other people’s articles.
https://blog.csdn.net/wwlcsdn000/article/details/79421612
Next, let's look at the introduction of these two methods separately.
Euler angles
The following article I think is very well written, listing the explanation of Euler angles and a clearer description of the universal lock.
https://blog.csdn.net/ronintao/article/details/52236210
Quaternion
The following article describes various ways of understanding and creating quaternions.
But the first one, I feel that there is a problem with the writing, one of the direction judgments, the vector definition method above the front , is not understood
correctly https://www.cnblogs.com/driftingclouds/p/6626183.html
The case is my test Code.
Detailed comments have been made. Create 4 cubes in the scene to see the effect.
public class QuaternionAPI : MonoBehaviour
{
public Transform m_t1;
public Transform m_t2;
public Transform m_t3;
public Transform m_t4;
void Start ()
{
//前方上方矢量界定法的实际上方会重新计算
//他的脸朝向哪儿 他的头顶朝向哪儿 两个参数的意义 请注意如果给定两个参数先判断是否正交
//如果正交才需要对齐 不正交的话默认是模型空间的正上方Y轴 请注意这个相交的意义 主要是判断第二个参数和Z轴是否相交
//因为第二个是基于模型空间来讲
//第一个参数的对齐哪个方向是相对于世界空间来讲 第二个参数是定义哪个是向上的方向 是相对于模型空间来讲
//m_t1.transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.up);
//m_t2.transform.rotation = Quaternion.LookRotation(Vector3.forward, new Vector3(0.5f, 0.5f, 0));
//m_t3.transform.rotation = Quaternion.LookRotation(Vector3.forward, new Vector3(0.5f, -0.5f, 0));
//m_t4.transform.rotation = Quaternion.LookRotation(Vector3.forward);
//m_t1.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f), Vector3.up);
//m_t2.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f), new Vector3(0, 0.5f, -0.5f));
//m_t3.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f), new Vector3(0, 0.5f, 0.5f));
//m_t4.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f));
m_t1.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f), Vector3.up);
m_t2.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f), new Vector3(0.5f, 0.5f, 0));
m_t3.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f), new Vector3(-0.5f, 0.5f, 0));
m_t4.transform.rotation = Quaternion.LookRotation(new Vector3(0, 0.5f, 0.5f));
//旋转量的4种表示形式
//这些都是相对于世界空间来说
//Quaternion q1=Quaternion.Euler(90, 0, 0);//绕X轴旋转 90度
//Quaternion q2 = Quaternion.LookRotation(Vector3.down);//人朝向下方
//Quaternion q3 = Quaternion.AngleAxis(90,Vector3.right);//绕右边的轴 X轴旋转90度
//Quaternion q4 = Quaternion.FromToRotation(Vector3.up, Vector3.forward);//将上方 旋转到 前方
//Quaternion q1 = Quaternion.Euler(0, 90, 0);//绕Y轴旋转 90度 注意旋转的正方向
//Quaternion q2 = Quaternion.LookRotation(Vector3.right);//人朝向左方
//Quaternion q3 = Quaternion.AngleAxis(90, Vector3.up);//绕上边的轴 Y轴旋转90度
//Quaternion q4 = Quaternion.FromToRotation(Vector3.left, Vector3.forward);//将上方 旋转到 前方
//Quaternion q1 = Quaternion.Euler(0, 0, 0);
//Quaternion q2 = Quaternion.LookRotation(Vector3.forward);//人朝向左方
//showQ("q1",q1);
//showQ("q2",q2);
//showQ("q3",q3);
//showQ("q4",q4);
}
How to use quaternions
https://jingyan.baidu.com/article/4ae03de3dbbac83eff9e6b00.html
https://blog.csdn.net/u014086857/article/details/51777112