向量的大小:向量各分量的平方和的平方根

API:float dis=vector.magnitude
代码实现:
//模长
void Demo01()
{
Vector3 pos = this.transform.position;
Debug.DrawLine(Vector3.zero,this.transform.position);
//利用数学公式求模长
float m1 =Mathf.Sqrt( Mathf.Pow(pos.x,2)+Mathf.Pow(pos.y,2)+Mathf.Pow(pos.z,2));
//利用Unity封装的方法(API)求模长
float m2 = pos.magnitude;
//利用距离公式求模长
float m3 = Vector3.Distance(Vector3.zero,pos);
Debug.LogFormat("{0},{1},{2}",m1,m2,m3);
}
向量的方向:
获取向量也称为“标准化向量”或“归一化向量”,即获取该向量的单位向量
单位向量:大小为1的向量
几何意义:将该向量拉长或缩短,使模长等于1
公式:V/|V|
vector2为vector1的单位向量
vector1.Normalized();将vector自身设置为单位向量
API:Vector3 Vector2=vector1.normalized;
代码实现:
//方向
void Demo02()
{
Vector3 pos = this.transform.position;
Debug.DrawLine(Vector3.zero, this.transform.position);
//数学公式
Vector3 dir1 = pos / pos.magnitude;
//UnityAPI 封装好的方法
Vector3 dir2 = pos.normalized;
Debug.DrawLine(Vector3.zero,dir1,Color.red);
}