向量
Debug.DrawLine(Vector3.zero, pos);
在 Debug 函数中有一个 DrawLine 的方法,只能在编辑器中显示。
float magnitude_01 = Mathf.Sqrt(Mathf.Pow(pos.x, 2) + Mathf.Pow(pos.y, 2) + Mathf.Pow(pos.z, 2));
float magnitude_02 = pos.magnitude;
float magnitude_03 = Vector3.Distance(Vector3.zero, pos);
以上三行代码都表示向量的模长(大小),第一行是公式 m = x 2 + y 2 + z 2 2 m = \sqrt[2]{x^2 + y^2 + z^2} m=2x2+y2+z2 计算;第二行是 Unity 封装好的方法;第三行也是 Unity 封装好的方法,更适合两点间间距。三者计算结果一致。
Vector3 direction_01 = pos / pos.magnitude;
Vector3 direction_02 = pos.normalized;
以上两行代码是为了表示向量的方向(计算单位向量),第一个是公式,第二个是 Unity 封装好的方法。
Vector3 relativeDirection = t1.position - t2.position;
//方向:指向被减量(前面的)
//大小:两点间的距离
//注意:实际位置要平移到坐标原点
//t3 沿着 relativeDirection 方向移动(只需要方向,就 normalized 一下,避免两个物体间距对速度造成影响)
if (Input.GetKeyDown(KeyCode.A))
t3.Translate(relativeDirection.normalized);
角度与弧度
float radian_01 = degree * Mathf.PI / 180;
float radian_02 = degree