Vector3 三维向量
表示3D的向量和点
Vector3.Angle 角度
由from和to两向量返回一个角度。形象的说,from和to的连线和它们一个指定轴向的夹角
public Transform target; void Update() { Vector3 targetDir = target.position - transform.position; Vector3 forward = transform.forward; float angle = Vector3.Angle(targetDir, forward); if (angle < 5.0F) print("close"); }
Vector3.back 向后
写Vector3(0, 0, -1)的简码,也就是向z轴负方向
void Example() { transform.position += Vector3.back * Time.deltaTime; }
Vector3.ClampMagnitude 限制长度
返回原向量的拷贝,并且它的模最大不超过maxLength所指示的长度。
也就是说,限制向量长度到一个特定的长度。
public Vector3 centerPt; public float radius; void Update() { Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); Vector3 newPos = transform.position + movement; Vector3 offset = newPos - centerPt; transform.position = centerPt + Vector3.ClampMagnitude(offset, radius); }
Vector3.Cross 叉乘
两个向量的交叉乘积
Vector3 GetNormal(Vector3 a, Vector3 b, Vector3 c) { Vector3 side1 = b - a; Vector3 side2 = c - a; return Vector3.Cross(side1, side2).normalized; }
Vector3.Distance 距离
返回a和b之间的距离
public Transform other; void Example() { if (other) { float dist = Vector3.Distance(other.position, transform.position); print("Distance to other: " + dist); } }
Vector3.Dot 点乘
两个向量的点乘积
public Transform other; void Update() { if (other) { Vector3 forward = transform.Tran