表示3D的向量和點檩奠。
這個結(jié)構(gòu)用于在Unity傳遞3D位置和方向。它也包含做些普通向量運算的函數(shù)附帽。
除了下面列出的函數(shù)埠戳,其他類用于處理向量和點。例如Quaternion和Matrix4x4類用于旋轉(zhuǎ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類實例屬性
1、normalized:單位化向量
public Vector3 normalized{ get; }
此屬性用來獲取Vector3實例的單位向量荚藻,即返回向量的方向與原方向相同屋灌,而模長變?yōu)?。
此屬性和實例方法Normalized( )的區(qū)別:
設(shè)A应狱、C均為Vector3實例共郭,則:
執(zhí)行代碼C=A.normalized后只是將向量A的單位向量賦給向量C,而向量A自身未變
執(zhí)行代碼A=Normalize()便會將向量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實例模長的平方值璃岳,由于計算開方值比較消耗資源年缎,在非必要情況下,可以考慮用
sqrMagnitude代替屬性magnitude铃慷,例如比較兩個向量長度的大小
B单芜、Vector3類實例方法
1、Scale:向量放縮
public void Scale(Vector3 scale);
此方法可以對Vector3實例按參照向量scale進(jìn)行放縮犁柜,注意與靜態(tài)方法Scale(a:Vector3洲鸠,b:Vector3)的區(qū)別:
實例方法直接更改實例的值,靜態(tài)方法將值賦給新的實例
C馋缅、Vector3類靜態(tài)方法
1坛怪、Angle:求兩個向量夾角
public static float Angle(Vector3 from, Vector3 to );
返回向量from和to的夾角,單位為角度股囊,返回值的范圍為[0,180]袜匿,且當(dāng)from和to至少一個為Vector.zero時,返回值為90
2稚疹、ClampMagnitude:向量長度
public static Vector3 ClampMagnitude(Vector3 vector居灯,float maxLength);
此方法用于返回向量vector3的一個同方向向量,其模長受maxLength的限制
返回向量的方向和vector方向相同
當(dāng)maxLength大于vector的模長時内狗,返回向量與vector相同
當(dāng)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的方向旋轉(zhuǎn),大拇指指向的方向是c的方向
4车摄、Dot:向量點乘
public static float Dot(Vector3 lhs, Vector3 rhs);
此方法用于返回參數(shù)lhs和rhs的點乘
c=Vector3.Dot( a,b );
c=|a|*|b|cos(e)
在實際開發(fā)中寺谤,通常利用點乘來確定兩個物體的相對位置關(guān)系,例如敵人相對主角的位置關(guān)系
5吮播、Lerp:向量插值
public static Vector3 Lerp(Vector3 from, Vector3 to, float t);
此方法用于返回一個參數(shù)from到to的線性插值向量
C=Vector3.Lerp(A变屁,B,t)
當(dāng)t<=0時意狠,向量C=A敞贡;
當(dāng)t>=1時,向量C=B摄职;
當(dāng)0
6誊役、MoveTowards:向量插值
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
此方法用于返回一個從參數(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:兩個坐標(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個坐標(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ù)面,到底垂直于哪個面由初始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取反時狡赐,反射向量不受影響
入射向量、反射向量和鏡面向量共面
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的乘積,注意和實例方法a.Scale(b)的區(qū)別
13拐纱、Slerp:球形插值
public static Vector3 Slerp(Vector3 from铜异,Vector3 to,float t);
此方法用于返回參數(shù)from點到參數(shù)to點的球形插值向量秸架,參數(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:阻尼移動
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為終點坐標(biāo),currentVelocity為當(dāng)前幀移動向量艾猜,參數(shù)smoothTime為接近目標(biāo)的阻尼強度截珍,
參數(shù)maxSpeed為最大移動速度,默認(rèn)值為無窮大箩朴,參數(shù)deltaTime為控制當(dāng)前幀實際移動距離岗喉,即為maxSpeed*deltaTime,
默認(rèn)值為Time.deltaTime
此方法用于模擬GameObject對象從current點到target點之間的阻尼運動
D炸庞、Vector3類運算符
1钱床、operator==
用于判斷向量是否足夠接近或相等
經(jīng)驗總結(jié):
控制游戲?qū)ο笙蚰繕?biāo)點移動有三種方法:
(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);
}
三者之間的不同點:
用Lerp方法控制游戲?qū)ο笙蚰繕?biāo)點移動事期,其是按比例進(jìn)行移動的,只能無限接近纸颜,不能到達(dá)目標(biāo)位置點兽泣。
用MoveTowards方法控制游戲?qū)ο笙蚰繕?biāo)點移動,每次移動一個設(shè)定的步長胁孙,其可以到達(dá)目標(biāo)位置點唠倦。
用Distance和Translate方法控制游戲?qū)ο笙蚰繕?biāo)點移動,有很大的局限性涮较,其必須先要知道要移動的方向才可以稠鼻,況且只能是正方向,并且其也只能接近狂票,不能到達(dá)目標(biāo)點位置候齿。