今天的目标是Vector3 和部分 Transform。先说Vector3。
首先是Vector3的中英文API
Struct
Representation of 3D vectors and points.
表示3D的向量和点。
This structure is used throughout Unity to pass 3D positions and directions around. It also contains functions for doing common vector operations.
这个结构用于在Unity传递3D位置和方向。它也包含做些普通向量运算的函数。
Besides the functions listed below, other classes can be used to manipulate vectors and points as well. For example the Quaternion and the Matrix4x4 classes are useful for rotating or transforming vectors and points.
除了下面列出的函数,其他类用于处理向量和点。例如Quaternion和Matrix4x4类用于旋转或变换向量和点。
Variables变量
-
X component of the vector.
向量的X组件。 -
Y component of the vector.
向量的Y组件。 -
Z component of the vector.
向量的Z组件。 -
Access the x, y, z components using [0], [1], [2] respectively.
使用[0], [1], [2]分别访问组件x, y, z组件。简单来说就是用索引号代替x, y, z组件。 -
Returns this vector with a magnitude of 1 (Read Only).
返回向量的长度为1(只读)。 -
Returns the length of this vector (Read Only).
返回向量的长度(只读)。 -
Returns the squared length of this vector (Read Only).
返回这个向量的长度的平方(只读)。
Constructors构造器
-
Creates a new vector with given x, y, z components.
创建一个新的具有给定x, y, z组件的向量。
Functions函数
-
Multiplies every component of this vector by the same component of scale.
由缩放的相同的组件对应乘以这个矢量的每个组件。 -
Makes this vector have a magnitude of 1.
使向量的长度为1。 -
Returns a nicely formatted string for this vector.
返回此向量格式化的字符串。
Class Variables类变量
Class Functions类函数
-
Linearly interpolates between two vectors.
两个向量之间的线性插值。 -
Spherically interpolates between two vectors.
球形插值在两个向量之间。 -
Makes vectors normalized and orthogonal to each other.
使向量规范化并且彼此相互垂直。 -
Moves a point current towards target.
当前的地点移向目标。 -
Rotates a vector current towards target.
当前的向量转向目标。 -
Gradually changes a vector towards a desired goal over time.
随着时间的推移,逐渐改变一个向量朝向预期的目标。 -
Multiplies two vectors component-wise.
两个矢量组件对应相乘。 -
Cross Product of two vectors.
两个向量的交叉乘积。返回lhs x rhs -
Reflects the vector along the normal.
沿着法线反射向量。 -
Dot Product of two vectors.
两个向量的点乘积。 -
Projects a vector onto another vector.
投影一个向量到另一个向量。 -
Returns the angle in degrees between from and to.
由from和to两者返回一个角度。 -
Returns the distance between a and b.
返回a和b之间的距离。 -
Returns a copy of vector with its magnitude clamped to maxLength.
返回向量的长度,最大不超过maxLength所指示的长度。 -
Returns a vector that is made from the smallest components of two vectors.
返回一个由两个向量的最小组件组成的向量。 -
Returns a vector that is made from the largest components of two vectors.
返回一个由两个向量的最大组件组成的向量。 -
Adds two vectors.
两个向量相加。 -
Subtracts one vector from another.
一个向量减另一个向量。 -
Multiplies a vector by a number.
由一个数乘以一个向量。 -
Divides a vector by a number.
由一个数除一个向量。也就是a/b。 -
Returns true if the vectors are equal.
如果两个向量相等,返回真。 -
Returns true if vectors different.
如果向量不同返回真。
Description
Multiplies every component of this vector by the same componentof
所有的轴向都乘以scale
static function Scale (a :Vector3, b : Vector3) : Vector3
Description
Multiplies two vectors component-wise.
Every component in the result is a componentof
Description
Makes this vector have a
让向量的长度变成1
When normalized, a vector keeps the same direction but its lengthis 1.0.
当normalized, 向量的方向不会变但是长度会变成1,0
Note that this function will change the current vector. If you wantto keep the current vector unchanged, use
这个函数会改变使用这个函数的向量,如果你不想改变这个向量,应该使用normalized
If this vector is too small to be normalized it will be set tozero
如果这个向量太小了,他不适合normalized并会被设置成zero (0,0,0)
Description
Returns a nicely formatted string for this vector
返回x,y,z的值,一般到小数点后一位。(0.0, 0.0, 0.0)
Class Variables
zero=(0,0,0)
one=(1,1,1)
forward=(0,0,1)
up=(0,1,0)
right=(1,0,0)
还有一个文档没写但也可以用的left=(-1,0,0)
Class Functions
1)static
Description
Linearly interpolates between two vectors.
Interpolates
t
跟Quaternion里的lerp一样,返回from 和to之间的一个Vector3 点p
2)static
Description
Spherically interpolates between two vectors.
Interpolates
t
作用跟lerp差不多,但是不是呈直线靠近。
3)static
Description
Makes vectors normalized and orthogonal to each other.
Normalizes
See Also:
这个函数读入两个向量normal和tangent, 使tangent 在由tangent和normal构成的平面上,并与normal垂直。两个向量都被Normalize,也就是长度为1。大家可以用这个脚本在编辑器中看出这个函数是如何工作的。
static function OrthoNormalize(ref normal : Vector3, ref tangent : Vector3, ref binormal :Vector3) : void
Description
Makes vectors normalized and orthogonal to each other.
Normalizes
See Also:
Description
Rotates a vector/from/towards
The vector will be rotated on an arc instead of being interpolatedlinearly. This is essentially the same as
产生一个向量从from旋转并移动到 to,跟Vector3.Slerp类似,但是可以用maxRadiansDelta和maxMagnitudeDelta分别控制向量的旋转方向和长度。当maxRadiansDelta=0时,向量指向from, 1则指向to。当maxMagnitudeDelta=0时,向量的长度=from.magnitude,1则=to.magnitude。向量的最大有效取值范围为1, 但是最小取值范围可以小于0
maxMagnitudeDelta, maxRadiansDelta [0,-∞]
当maxRadiansDelta和maxMagnitudeDelta为负时,向量会向相反方向旋转和延长。
5)function
Description
Multiplies every component of this vector by the same componentof
将两个向量的对应轴相乘。
a,b ∈Vector3
a.Scale(b);
a.x=a.x * b.x
static function Scale (a :Vector3, b : Vector3) : Vector3
Description
Multiplies two vectors component-wise.
Every component in the result is a componentof
跟上一个差不多
a,b,c ∈Vector3
a.x=b.x * c.x
6)static
Description
Cross Product of two vectors.
Returns
两个向量的叉积,具体关于叉积的解释大家可以自己去网上搜搜,或点击传送门。
http://zh.wikipedia.org/zh/向量积
http://baike.baidu.com/view/865221.htm
-
a
= a 1 i + a 2 j + a 3 k =[ a 1, a 2, a 3]
-
b
= b 1 i + b 2 j + b 3 k =[ b 1, b 2, b 3]
则
-
a
× b =[a 2b 3 −a 3b 2,a 3b 1 −a 1b 3,a 1b 2 −a 2b 1]
7)static
Description
Reflects the vector along the normal.
The returned valueis
又一个很常用的函数,返回一个向量,让这个向量与另一个向量inDirection以坐标轴inNormal为准镜像。
Description
Dot Product of two vectors.
Returns
For
For vectors of arbitrary length the Dot return values are similar:they get larger when the angle between vectors decreases.
点积,跟quaternion里的用法一样。对于normalized后的lhs和rhs,如果指向相同的方向,返回1。返回-1如果他们指向完全相反的方向。其他情况下根据角度返回两者之间的小数。如果两个向量互相垂直,返回0;
两个 向量u,v的点积是一个标量,用u·v表示。通用公式:|u||v|cos<u,v>。在三维空间中代数公式:uxvx+ uyvy +uzvz。(该公式可以先由二维证明,再推广到多维。二维中的证明:利用点线距公式和勾股定理推出|u|*cos<u,v>的表达式,再根据定义化简即可。) 点积的值由以下三个值确定: u的大小v的大小u,v夹角的余弦。在u,v非零的前提下,点积如果为负,则u,v形成的角大于90度;如果为零,那么u,v垂直;如果为正,那么u,v形成的角为锐角。 点积得到两个向量的夹角的cos值,通过它可以知道两个向量的相似性,利用点积可判断一个多边形是否面向摄像机还是背向摄像机
9)static
Description
Projects a vector onto another vector.
Returns
投射一个向量到另一个。
返回一个向量,这个向量由vector投射到onNormal。 返回0如果onNormal几乎等于或等于0;
具体工作原理可以通过这个脚本了解到
10)static
Description
Returns the angle in degreesbetween
这个基本上是我用的非常多的一个函数,用来确定两个向量指向的位置间的夹角
返回从from到to的夹角,单位为度。
我一般用这个来测量坦克之类的车辆的y轴和空间坐标y轴之间的夹角,阻止其登上大于固定角度的斜坡。或者ai只会看见正前方多少度内的敌人,因为他不应该拥有360度的视野。
11)static
Description
Returns the distancebetween
Vector3.Distance(a,b)
返回从a到b的距离
使用这个函数Vector3.Distance(a,b)的结果跟直接在两个向量间作减法(a-b)后求出的向量的长度是一样的。
也是非常非常常用到的一个函数。
12)static
Description
Returns a vector that is made from the smallest componentsof
返回lhs和rhs中最小的那个向量
13)static
Description
Returns a vector that is made from the largest componentsof
返回lhs和rhs中最大的那个向量
这两个也是很常用的。
14)operators
+ 和-
两个向量相加和相减
*和/
一个向量的所有轴乘以或除以一个float或int, 得到一个新的向量