Vector3类属于结构体类型,用来表示Unity中的三维向量或三维坐标点
Vector3类实例属性
在Vector3类中,涉及的实例属性有normalized属性和sqrMagnitude属性
normalized属性:单位化向量
基本语法
public Vector3 normalized{ get; }
功能说明
此属性用来获取Vector3实例的单位向量,即返回向量的方向与原向量方向相同,而模长变为1
sqrMagnitude属性:模长平方
基本语法
此属性用于返回Vector3实例模长的平方值
Vector3类实例方法
在Vector3类中涉及的实例方法只有Scale方法,由于Vector3的静态方法Scale与实例方法scale的功能相近。
Scale方法:向量放缩
基本语法
public void Scale(Vector3 scale);
其中参数scale的参考向量
功能说明
此方法可以对Vector3实例按参考向量scale进行放缩
Vector3类静态方法
在Vector3类中,静态方法有Angle方法、ClampMagnitude方法、Cross方法、Dot方法、Lerp方法、MoveTowards方法、OrthoNormalize方法、Project方法、RotateTowards方法、Scale方法、Slerp方法和SmoothDamp方法。
Angle方法:求两个向量夹角
基本语法
public static float Angle(Vector3 from ,Vector3 to);
功能说明
此方法用于返回向量from和to的夹角,单位为角度,返回值的范围为[0,180],且当from和to中至少有一个味Vector.zero时,方法返回值为90
ClampMagnitude方法:向量长度
基本语法
public static Vector3 ClampMagnitude(Vector3 vector ,float maxLength);
功能说明
此方法用于返回向量vector的一个同方向向量,其模长受maxLength的限制。
1、返回向量的方向与vector方向相同
2、当maxLength大于vector的模长时,返回向量与vector相同
3、当maxLength小于vector的模长时,返回向量的模长等于maxLength,但方向与vector相同。
Cross方法:向量叉乘
基本语法
public static Vector3 Cross(Vector3 lhs , Vector3 rhs);
功能说明:此方法用于两个向量的叉乘
Dot方法:向量点乘
基本语法
public static float Dot(Vector3 lhs ,Vector3 rhs)
功能说明
此方法用于返回参数lhs和rhs的点乘。
Lerp方法:向量插值
基本语法
public static Vector3 Lerp(Vector3 from ,Vector3 to ,float t);
其中参数from为插值起始点坐标,参数to为插值结束点坐标,参数t为插值系数
功能说明
此方法用于返回一个从参数from到to的线性插值向量。
MoveTowards方法:向量插值
基本语法
public static Vector3 MoveTowards(Vector3 current ,Vector3 target ,float maxDistanceDelta);
功能说明
此方法用于返回一个从参数current到参数target的插值向量
OrthoNormalize方法:两个坐标轴的正交化
基本语法
public static void OrthoNormalize(ref Vector3 normal ,ref Vector3 tangent);
功能说明
此方法用于对向量normal进行单位化处理,并对向量tangent进行正交化处理
OrthoNormalize方法:3个坐标轴的正交化
基本语法
public static void OrthoNormalize(ref Vector3 normal ,ref Vector3 tangent ,ref Vector3 binormal);
功能说明
此方法用于对向量normal进行单位化处理,并对向量tangent和binnormal进行正交化处理
Project方法:投影向量
基本语法
public static Vector3 Project(Vector3 vector, Vector3 onNormal);
功能说明:此方法用于返回向量vector在向量onNormal的投影向量
Reflect方法:反射向量
基本语法
public static Vector3 Reflect(Vector3 inDirection ,Vector3 inNormal);
其中参数inDirection为入射向量,参数inNormal为镜面向量
功能说明
此方法用于返回向量inDirection的反射向量
1、参数inNormal向量必须为单位向量,否则入射角和反射角不相等
2、当inNormal取反时,反射向量不受影响
3、入射向量、反射向量和镜面向量共面
RotateToward方法:球形插值
基本语法
public static Vector3 RotateTowards(Vector3 current ,Vector3 target,float maxRadiansDelta,float maxMagnitudeDelta);
其中参数current为起始点坐标,参数target为目标点坐标,参数maxRadiansDelta为角度旋转系数,参数maxMagnitudeDelta为模长系数
功能说明
此方法用于返回参数current到target的球形旋转插值向量,此方法可控制插值向量的角度和模长
Scale方法:向量放缩
基本语法
public static Vector3 Scale(Vector3 a ,Vector3 b);
功能说明
此方法用于返回向量a和b的乘积
Slerp方法:球形插值
基本语法
public static Vector3 Slerp(Vector3 from,Vector3 to ,float t);
其中参数from为插值起始点坐标,参数to为插值结束点坐标,参数t为插值系数。
功能说明:此方法用于返回从参数from点到参数to点的球形插值向量
SmoothDamp方法:阻尼移动
基本语法
1、public static Vector3 SmoothDamp(Vector3 current ,Vector3 target,ref Vector3 currentVelocity ,float smoothTime);
2、public static Vector3 SmoothDamp(Vector3 current ,Vector3 target,ref Vector3 currentVelocity, float smoothTime ,float maxSpeed);
3、public static Vector3 SmoothDamp(Vector3 current ,Vector3 target , ref Vector3 currentVelocity ,float smoothTime ,float maxSpeed,float deltaTime);
其中参数current为起点坐标,参数target为终点坐标,参数currentVelocity为当前帧移动向量,参数smoothTime为接近目标时的阻尼强度,参数maxSpeed为最大移动速度,默认值为无穷大,参数deltaTime为控制当前帧实际移动的距离,即为maxSpeed*deltaTime,默认值为Time.deltaTime。
Vector3类运算符
在Vector3类中,涉及的运算符主要有相等(“==”)运算符
operator == (lhs:Vector3 ,rhs:Vector3)
功能说明:此运算符用于判断向量lhs和rhs是否足够接近或相等。