链接:http://docs.unity3d.com/ScriptReference/Vector3.html
struct in UnityEngine
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.
此结构用来贯穿整个Unity3D位置和方向的转换。也包括一般向量操作方法。
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……
public Vector3(float x, float y, float z);
public void Set(float new_x, float new_y, float new_z);
Set函数
public string ToString();
public string ToString(string format);
返回向量对应的格式化字符串
operator - 向量相减
operator != 向量判等,返回bool
operator * 数乘
operator / 数除
operator + 向量相加
operator == 向量判等,返回bool
public static float Angle(Vector3 from, Vector3 to);
返回两向量间角度,不是弧度,数值不超过180。
public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);
限制向量vector长度为maxLength,若vector.magnitude > maxLegth, 返回值叫vector缩小
public static float Distance(Vector3 a, Vector3 b);
Distance函数返回向量的距离,为(a-b).magnitude
public static float Dot(Vector3 lhs, Vector3 rhs);
两向量点乘,为(|a| * |b| * cos(angle))
public static Vector3 Lerp(Vector3 from, Vector3 to, float t);
线性插值两个向量,t=0,返回from,t=1,返回to,一般采用Vector3.Lerp(startPoint, endPoint, Time.deltaTime * xx)实现平滑移动
public static Vector3 Max(Vector3 lhs, Vector3 rhs);
public static Vector3 Min(Vector3 lhs, Vector3 rhs);
返回两向量x,y,z对应坐标的最大或者最小值
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
current向 target移动,maxDistanceDelta为每秒的距离
public void Normalize();
取单位向量。
public static Vector3 Project(Vector3 vector, Vector3 onNormal);
public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent);
取normal的单位向量,然后取tangent的单位向量,并与normal正交。
public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent, ref Vector3 binormal);
去normal的单位向量,然后去tangent的单位向量,并与normal正交;去binormal单位向量,并与normal和tangent分别正交。
public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
投影vector到以planeNormal为法向量的平面上。
public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal);
反射indirection,以inNormal为法向量的平面为反射面。
public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);
与MoveTowards不同之处在于这里current代表的是方向而非点。
public static Vector3 Scale(Vector3 a, Vector3 b);
(1,2,3)*(2,3,4) = (2,6,12)
public static Vector3 Slerp(Vector3 from, Vector3 to, float t);
球面插值
public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);
平滑阻尼,向量由一些像弹簧阻尼器函数平滑,这将永远不会超过。最常见的用途是平滑跟随相机,例如下:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform target;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
void Update() {
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
}