热度 2||
今天的目标是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变量Multiplies two vectors component-wise.
Every component in the result is a componentof a multiplied bythe same component of b.
Makes this vector have a magnitude of1.
让向量的长度变成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 variable.
这个函数会改变使用这个函数的向量,如果你不想改变这个向量,应该使用normalized
If this vector is too small to be normalized it will be set tozero
如果这个向量太小了,他不适合normalized并会被设置成zero (0,0,0)
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 function Lerp(from : Vector3, to : Vector3, t : float) : Vector3
DescriptionLinearly interpolates between two vectors.
Interpolates from towards to byamount t.
t is clamped between [0...1].When t = 0returns from.When t = 1returns to.When t = 0.5 returnsthe averageof from and to.
跟Quaternion里的lerp一样,返回from 和to之间的一个Vector3 点p p=form+(to-form)*t t[0,1]
2)static function Slerp (from: Vector3, to : Vector3, t : float) : Vector3
DescriptionSpherically interpolates between two vectors.
Interpolates from towards to byamount t. The returnedvector's magnitude willbe interpolated between magnitudesof from and to.
t is clamped between [0...1].
作用跟lerp差不多,但是不是呈直线靠近。
3)static function OrthoNormalize(ref normal : Vector3, ref tangent : Vector3): void
Makes vectors normalized and orthogonal to each other.
Normalizes normal.Normalizes tangent andmakes sure it is orthogonalto normal (that is,angle between them is 90 degrees).
See Also: Normalize function.
这个函数读入两个向量normal和tangent, 使tangent 在由tangent和normal构成的平面上,并与normal垂直。两个向量都被Normalize,也就是长度为1。大家可以用这个脚本在编辑器中看出这个函数是如何工作的。
Makes vectors normalized and orthogonal to each other.
Normalizes normal.Normalizes tangent andmakes sure it is orthogonalto tangent.Normalizes binormal andmakes sure it is orthogonal toboth normal and tangent.
See Also: Normalize function.
这个用法多了一个向量binormal,这个向量同时垂直于normal和tangent,三者关系类似三维的直角坐标轴。其中binormal的方向决定binormal在那边垂直于normal和tangent。
Rotates a vector/from/towards to.
The vector will be rotated on an arc instead of being interpolatedlinearly. This is essentially the same as Vector3.Slerp butinstead the function will ensure that the angular speed and changeof magnitude neverexceeds maxRadiansDelta and maxMagnitudeDelta.
产生一个向量从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 Scale(scale : Vector3) : void
Multiplies every component of this vector by the same componentof scale.
将两个向量的对应轴相乘。
a,b ∈Vector3
a.Scale(b);
a.x=a.x * b.x a.y=a.y*b.y a.z=a.z*b.z
Multiplies two vectors component-wise.
Every component in the result is a componentof a multiplied bythe same component of b.
跟上一个差不多
a,b,c ∈Vector3
a=Vector3.Scale(b,c);
a.x=b.x * c.x a.y=b.y*c.y a.z=b.z*c.z
6)static function Cross (lhs: Vector3, rhs : Vector3) : Vector3
DescriptionCross Product of two vectors.
Returns lhs x rhs.
两个向量的叉积,具体关于叉积的解释大家可以自己去网上搜搜,或点击传送门。
http://zh.wikipedia.org/zh/向量积
http://baike.baidu.com/view/865221.htm
a = a1i + a2j + a3k =[a1, a2, a3]b = b1i + b2j + b3k =[b1, b2, b3]
则
a × b =[a2b3 −a3b2,a3b1 −a1b3,a1b2 −a2b1]
7)static function Reflect(inDirection : Vector3, inNormal : Vector3) : Vector3
DescriptionReflects the vector along the normal.
The returned valueis inDirection reflectedfrom a surface with anormal inNormal.
又一个很常用的函数,返回一个向量,让这个向量与另一个向量inDirection以坐标轴inNormal为准镜像。
8)static function Dot(lhs : Vector3, rhs : Vector3) : float
DescriptionDot Product of two vectors.
Returns lhs . rhs.
For normalized vectorsDot returns 1 if they point in exactly the same direction; -1 ifthey point in completely opposite directions; and a number inbetween for other cases (e.g. Dot returns zero if vectors areperpendicular).
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值,通过它可以知道两个向量的相似性,利用点积可判断一个多边形是否面向摄像机还是背向摄像机一般情况下还是Vector3.Angle()这个函数用的比较多,两者的功能基本是一样的。
9)static function Project(vector : Vector3, onNormal : Vector3) : Vector3
DescriptionProjects a vector onto another vector.
Returns vector projectedonto onNormal. Returns zero vectorif onNormal isalmost zero.
投射一个向量到另一个。
返回一个向量,这个向量由vector投射到onNormal。 返回0如果onNormal几乎等于或等于0;
具体工作原理可以通过这个脚本了解到
10)static function Angle (from: Vector3, to : Vector3) : float
DescriptionReturns the angle in degreesbetween from and to.
这个基本上是我用的非常多的一个函数,用来确定两个向量指向的位置间的夹角
返回从from到to的夹角,单位为度。
我一般用这个来测量坦克之类的车辆的y轴和空间坐标y轴之间的夹角,阻止其登上大于固定角度的斜坡。或者ai只会看见正前方多少度内的敌人,因为他不应该拥有360度的视野。
11)static function Distance (a: Vector3, b : Vector3) : float
DescriptionReturns the distancebetween a and b.
Vector3.Distance(a,b) isthe same as (a-b).magnitude
返回从a到b的距离
使用这个函数Vector3.Distance(a,b)的结果跟直接在两个向量间作减法(a-b)后求出的向量的长度是一样的。
也是非常非常常用到的一个函数。
12)static function Min(lhs : Vector3, rhs : Vector3) : Vector3
DescriptionReturns a vector that is made from the smallest componentsof lhs and rhs.
返回lhs和rhs中最小的那个向量
13)static function Max (lhs :Vector3, rhs : Vector3) : Vector3
DescriptionReturns a vector that is made from the largest componentsof lhs and rhs.
返回lhs和rhs中最大的那个向量
这两个也是很常用的。
14)operators
+ 和-
两个向量相加和相减
*和/
一个向量的所有轴乘以或除以一个float或int, 得到一个新的向量
小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )
GMT+8, 2025-4-26 08:18 , Processed in 0.294998 second(s), 17 queries .
Powered by Discuz! X3.5 Licensed
© 2001-2025 Discuz! Team.