【Unity】入门学习笔记180526——API(20)——Vector3类

Vector3类

Vector3类属于结构体类型,用来表示Unity中的三维向量或三维坐标点。

介绍Vector3类的一些实例属性、实例方法、静态方法和运算符


A、Vector3类实例属性

1、normalized:单位化向量

public Vector3 normalized{ get; }

此属性用来获取Vector3实例的单位向量,即返回向量的方向与原方向相同,而模长变为1。

此属性和实例方法Normalized( )的区别:

设A、C均为Vector3实例,则:

执行代码C=A.normalized后只是将向量A的单位向量赋给向量C,而向量A自身未变

执行代码A=Normalize()便会将向量A进行单位化处理,使得原向量A变成了单位向量

执行代码C=Vector3.Normalize(A)的结果与执行代码C=A.normalized的相同,即只是将A的单位向量赋给了向量C,而向量A未被改变,因此编程中常用代码C=A.normalized代替。


2、sqrMagnitude:模长平方

public float sqrMagnitude{ get; }

此属性用于返回Vector3实例模长的平方值,由于计算开方值比较消耗资源,在非必要情况下,可以考虑用

sqrMagnitude代替属性magnitude,例如比较两个向量长度的大小


B、Vector3类实例方法

1、Scale:向量放缩

public void Scale(Vector3 scale);

此方法可以对Vector3实例按参照向量scale进行放缩,注意与静态方法Scale(a:Vector3,b:Vector3)的区别:

实例方法直接更改实例的值,静态方法将值赋给新的实例


C、Vector3类静态方法

1、Angle:求两个向量夹角

public static float Angle(Vector3 from, Vector3 to );

返回向量from和to的夹角,单位为角度,返回值的范围为[0,180],且当from和to至少一个为Vector.zero时,返回值为90


2、ClampMagnitude:向量长度

public static Vector3 ClampMagnitude(Vector3 vector,float maxLength);

此方法用于返回向量vector3的一个同方向向量,其模长受maxLength的限制

返回向量的方向和vector方向相同

当maxLength大于vector的模长时,返回向量与vector相同

当maxLength小于vector的模长时,返回向量的模长等于maxLength,但方向与vector相同


3、Cross方法:向量叉乘

public static Vector3 Cross(Vector3 lhs,Vector3 rhs);

此方法用于求两个向量的叉乘,满足:

c⊥a,c⊥b;

|c|=|a|*|b|sin(e);

a,b,c满足右手法则,即四指指向b的方向,然后向a的方向旋转,大拇指指向的方向是c的方向


4、Dot:向量点乘

public static float Dot(Vector3 lhs, Vector3 rhs);

此方法用于返回参数lhs和rhs的点乘

c=Vector3.Dot( a,b );

c=|a|*|b|cos(e)

在实际开发中,通常利用点乘来确定两个物体的相对位置关系,例如敌人相对主角的位置关系


5、Lerp:向量插值

public static Vector3 Lerp(Vector3 from, Vector3 to, float t);

此方法用于返回一个参数from到to的线性插值向量

C=Vector3.Lerp(A,B,t)

当t<=0时,向量C=A;

当t>=1时,向量C=B;

当0<t<1时,向量C=A+(B-A)*t


6、MoveTowards:向量插值

public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);

此方法用于返回一个从参数current到参数target的插值向量。

C=Vector3.MoveTowards(A,B,sp);

向量C为:C=A+K*(B-A)

其中K=sp>(dx^2+dy^2+dz^2)^(1/2)?1:sp/(dx^2+dy^2+dz^2)^(1/2)


7、OrthoNormalize:两个坐标轴的正交化

public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent);

此方法用于对向量normal进行单位化处理,并对向量tangent进行正交化处理

Vector3.OrthoNormalize(ref v1, ref v2);

v1、v2变换为v3、v4

v3=v1.normalized

v4与v3垂直,且v4模长为1


8、OrthoNormalize:3个坐标轴的正交化

public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent,ref Vecotor3 binormal );

此方法用于对向量normal进行单位化处理,并对向量tangent和binormal进行正交化处理

向量binormal垂直于向量normal和tangent组成的平面,且向量binormal变换前后的夹角小于90度,即执行OrthoNormalize之后,bi'normal的方向可能垂直于由normal和tangent组成的平面的正面也可能是负面,到底垂直于哪个面由初始binormal的方向决定


9、Project:投影向量

public static Vector3 Project(Vector3 vector,Vector3 onNormal);

此方法用于返回向量vector在向量onNormal上的投影向量

projects=Vector3.Project(from_T.position,to_T.position);

projects为from_T在to_T方向上的投影向量,另外,向量to_T无需为单位向量


10、Reflect:反射向量

public static Vector3 Reflect(Vector3 inDirection,Vector3 inNormal);

其中,参数inDirection为入射向量,参数inNormal为镜面向量

此方法用于返回向量inDi'rection的反射向量

参数inNormal向量必须为单位向量,否则入射角和反射角不相等

当inNormal取反时,反射向量不受影响

入射向量、反射向量和镜面向量共面


11、RotateTowards:球形插值

public static Vector3 RotateTowards(Vector3 ccurrent, Vector3 target, float maxRadiansDelta,

float maxMagnitudeDelta);

其中参数current到target的球形旋转插值向量,此方法可控制插值向量的角度和模长


12、Scale:向量放缩

public static Vector3 Scale(Vector3 a, Vector3 b);

此方法用于返回向量a和b的乘积,注意和实例方法a.Scale(b)的区别


13、Slerp:球形插值

public static Vector3 Slerp(Vector3 from,Vector3 to,float t);

此方法用于返回参数from点到参数to点的球形插值向量,参数t范围为[0,1]

C=Vector3.Slerp(from,to,t);

K=e*(1-t)

|C|=(ax^2+ay^2+az^2)^(1/2)+[(bx^2+by^2+bz^2)^(1/2)-(ax^2+ay^2-az^2)^(1/2)]*t


14、SmoothDamp:阻尼移动

public static Vector3 SmoothDamp(Vector3 current,Vector3 target,ref Vector3 currentVelocity,

float smoothTime);

public static Vector3 SmoothDamp(Vector3 current,Vector3 target,ref Vector3 currentVelocity,

float smoothTime,float maxSpeed);

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

此方法用于模拟GameObject对象从current点到target点之间的阻尼运动


D、Vector3类运算符

1、operator==

用于判断向量是否足够接近或相等








猜你喜欢

转载自blog.csdn.net/dylan_day/article/details/80455939
今日推荐