表示3D的向量和點(diǎn)。
這個(gè)結(jié)構(gòu)用于在Unity傳遞3D位置和方向。它也包含做些普通向量運(yùn)算的函數(shù)。
除了下面列出的函數(shù),其他類用于處理向量和點(diǎn)。例如Quaternion和Matrix4x4類用于旋轉(zhuǎn)或變換向量和點(diǎn)。



1、Vector3.this[int] 操作索引
使用[0], [1], [2]分別訪問組件x, y, z組件。簡單來說就是用索引號代替x, y, z組件。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
? ? public Vector3 p;
? ? void Example() {
? ? ? ? p[1] = 5;
? ? }
}
API詳解
A、Vector3類實(shí)例屬性
1、normalized:單位化向量
public Vector3 normalized{ get; }
此屬性用來獲取Vector3實(shí)例的單位向量,即返回向量的方向與原方向相同,而模長變?yōu)?。
此屬性和實(shí)例方法Normalized( )的區(qū)別:
設(shè)A、C均為Vector3實(shí)例,則:
執(zhí)行代碼C=A.normalized后只是將向量A的單位向量賦給向量C,而向量A自身未變
執(zhí)行代碼A=Normalize()便會(huì)將向量A進(jìn)行單位化處理,使得原向量A變成了單位向量
執(zhí)行代碼C=Vector3.Normalize(A)的結(jié)果與執(zhí)行代碼C=A.normalized的相同,即只是將A的單位向量賦給了向量C,而向量A未被改變,因此編程中常用代碼C=A.normalized代替。
2、sqrMagnitude:模長平方
public float sqrMagnitude{ get; }
此屬性用于返回Vector3實(shí)例模長的平方值,由于計(jì)算開方值比較消耗資源,在非必要情況下,可以考慮用
sqrMagnitude代替屬性magnitude,例如比較兩個(gè)向量長度的大小
B、Vector3類實(shí)例方法
1、Scale:向量放縮
public void Scale(Vector3 scale);
此方法可以對Vector3實(shí)例按參照向量scale進(jìn)行放縮,注意與靜態(tài)方法Scale(a:Vector3,b:Vector3)的區(qū)別:
實(shí)例方法直接更改實(shí)例的值,靜態(tài)方法將值賦給新的實(shí)例
C、Vector3類靜態(tài)方法
1、Angle:求兩個(gè)向量夾角
public static float Angle(Vector3 from, Vector3 to );
返回向量from和to的夾角,單位為角度,返回值的范圍為[0,180],且當(dāng)from和to至少一個(gè)為Vector.zero時(shí),返回值為90
2、ClampMagnitude:向量長度
public static Vector3 ClampMagnitude(Vector3 vector,float maxLength);
此方法用于返回向量vector3的一個(gè)同方向向量,其模長受maxLength的限制
返回向量的方向和vector方向相同
當(dāng)maxLength大于vector的模長時(shí),返回向量與vector相同
當(dāng)maxLength小于vector的模長時(shí),返回向量的模長等于maxLength,但方向與vector相同
3、Cross方法:向量叉乘
public static Vector3 Cross(Vector3 lhs,Vector3 rhs);
此方法用于求兩個(gè)向量的叉乘,滿足:
c⊥a,c⊥b;
|c|=|a|*|b|sin(e);
a,b,c滿足右手法則,即四指指向b的方向,然后向a的方向旋轉(zhuǎn),大拇指指向的方向是c的方向
4、Dot:向量點(diǎn)乘
public static float Dot(Vector3 lhs, Vector3 rhs);
此方法用于返回參數(shù)lhs和rhs的點(diǎn)乘
c=Vector3.Dot( a,b );
c=|a|*|b|cos(e)
在實(shí)際開發(fā)中,通常利用點(diǎn)乘來確定兩個(gè)物體的相對位置關(guān)系,例如敵人相對主角的位置關(guān)系
5、Lerp:向量插值
public static Vector3 Lerp(Vector3 from, Vector3 to, float t);
此方法用于返回一個(gè)參數(shù)from到to的線性插值向量
C=Vector3.Lerp(A,B,t)
當(dāng)t<=0時(shí),向量C=A;
當(dāng)t>=1時(shí),向量C=B;
當(dāng)0
6、MoveTowards:向量插值
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
此方法用于返回一個(gè)從參數(shù)current到參數(shù)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:兩個(gè)坐標(biāo)軸的正交化
public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent);
此方法用于對向量normal進(jìn)行單位化處理,并對向量tangent進(jìn)行正交化處理
Vector3.OrthoNormalize(ref v1, ref v2);
v1、v2變換為v3、v4
v3=v1.normalized
v4與v3垂直,且v4模長為1
8、OrthoNormalize:3個(gè)坐標(biāo)軸的正交化
public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent,ref Vecotor3 binormal );
此方法用于對向量normal進(jìn)行單位化處理,并對向量tangent和binormal進(jìn)行正交化處理
向量binormal垂直于向量normal和tangent組成的平面,且向量binormal變換前后的夾角小于90度,即執(zhí)行OrthoNormalize之后,bi'normal的方向可能垂直于由normal和tangent組成的平面的正面也可能是負(fù)面,到底垂直于哪個(gè)面由初始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);
其中,參數(shù)inDirection為入射向量,參數(shù)inNormal為鏡面向量
此方法用于返回向量inDi'rection的反射向量
參數(shù)inNormal向量必須為單位向量,否則入射角和反射角不相等
當(dāng)inNormal取反時(shí),反射向量不受影響
入射向量、反射向量和鏡面向量共面
11、RotateTowards:球形插值
public static Vector3 RotateTowards(Vector3 ccurrent, Vector3 target, float maxRadiansDelta,
float maxMagnitudeDelta);
其中參數(shù)current到target的球形旋轉(zhuǎn)插值向量,此方法可控制插值向量的角度和模長
12、Scale:向量放縮
public static Vector3 Scale(Vector3 a, Vector3 b);
此方法用于返回向量a和b的乘積,注意和實(shí)例方法a.Scale(b)的區(qū)別
13、Slerp:球形插值
public static Vector3 Slerp(Vector3 from,Vector3 to,float t);
此方法用于返回參數(shù)from點(diǎn)到參數(shù)to點(diǎn)的球形插值向量,參數(shù)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:阻尼移動(dòng)
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為起始坐標(biāo),target為終點(diǎn)坐標(biāo),currentVelocity為當(dāng)前幀移動(dòng)向量,參數(shù)smoothTime為接近目標(biāo)的阻尼強(qiáng)度,
參數(shù)maxSpeed為最大移動(dòng)速度,默認(rèn)值為無窮大,參數(shù)deltaTime為控制當(dāng)前幀實(shí)際移動(dòng)距離,即為maxSpeed*deltaTime,
默認(rèn)值為Time.deltaTime
此方法用于模擬GameObject對象從current點(diǎn)到target點(diǎn)之間的阻尼運(yùn)動(dòng)
D、Vector3類運(yùn)算符
1、operator==
用于判斷向量是否足夠接近或相等
經(jīng)驗(yàn)總結(jié):
控制游戲?qū)ο笙蚰繕?biāo)點(diǎn)移動(dòng)有三種方法:
(1)、用Lerp方法(插值函數(shù)):
this.gameObject.transform.position=Vector3.Lerp(this.gameObject.transform.position,target.transform.position,speed);
(2)、用MoveTowards方法
this.gameObject.transform.position=Vector3.MoveTowards(this.gameObject.transform.position,?target.transform.position,speed);
(3)、用Distance和Translate方法
if(Vector3.Distance(this.gameObject.transform.position,target.transform.position)>0.1)
? ?{
??????this.gameObject.transform.Translate(target.transform.up);
}
三者之間的不同點(diǎn):
用Lerp方法控制游戲?qū)ο笙蚰繕?biāo)點(diǎn)移動(dòng),其是按比例進(jìn)行移動(dòng)的,只能無限接近,不能到達(dá)目標(biāo)位置點(diǎn)。
用MoveTowards方法控制游戲?qū)ο笙蚰繕?biāo)點(diǎn)移動(dòng),每次移動(dòng)一個(gè)設(shè)定的步長,其可以到達(dá)目標(biāo)位置點(diǎn)。
用Distance和Translate方法控制游戲?qū)ο笙蚰繕?biāo)點(diǎn)移動(dòng),有很大的局限性,其必須先要知道要移動(dòng)的方向才可以,況且只能是正方向,并且其也只能接近,不能到達(dá)目標(biāo)點(diǎn)位置。