使用矢量移动对象
了解矢量算法
添加

Vector a is a 2D vector with x component 3 and y component 5. Vector b is a 2D vector with x component 2 and y component -1. Adding a and b results in vector c, a 2D vector with x component 5 and y component 4. The relation of the three vectors to the origin point forms a scalene triangle.
var pointInAir = pointOnGround + new Vector2(0, 5);
减法

2D vector a is subtracted from 2D vector b resulting in a 2D vector with x component 5 and y component 4. This is illustrated as a single line with directional arrow from point a to point b.
// The vector d has the same magnitude as c but points in the opposite direction.var c = b - a;var d = a - b;
// These both give the same result.var c = a - b;var c = a + -b;
从一个Object到另一个Object的方向和距离
// Gets a vector that points from the player's position to the target's.var heading = target.position - player.position;
var distance = heading.magnitude;var direction = heading / distance; // This is now the normalized direction.
sqrMagnitudeif (heading.sqrMagnitude < maxRange * maxRange) { // Target is within range.}
var heading = target.position - player.position;return 0; // This is the overground heading.
标量乘法和除法
DOT 商品

Two 2D vectors represented as directional lines extend from the same origin point. Vector a extends straight upward with a positive y component but no x component. Vector b extends upward by a smaller y component than vector a and to the right with a positive x component. A line describing the region of the y axis covered by both vectors is labelled as a multiplied by b.

A series of vectors radiate out at angles between 0 and 180 degrees from a reference vector. Between 0 and 90 degrees from the reference vector, the dot product is between 1 and 0, and decreases as the angle increases. Between 90 and 180 degrees from the reference vector, the dot product is between 0 and -1, and decreases as the angle increases.
Mathf.Cosrigidbody.velocityvar fwdSpeed = Vector3.Dot(rigidbody.velocity, transform.forward);
差乘

A series of vectors radiate out at angles between 0 and 180 degrees from a reference vector. Between 0 and 90 degrees from the reference vector, the cross product is between 0 and 1, and increases as the angle increases. Between 90 and 180 degrees from the reference vector, the cross product is between 1 and 0, and decreases as the angle increases.
计算法线/垂直矢量
- 从三个点中选择其中一个点
- 将其分别从Other两个点中减去(产生两个新建矢量,“Side 1”和“Side 2”)
- 计算矢量“Side 1”和“Side 2”的差乘
- 差乘的结果是垂直于三个原始点 ON 所在的平面的新建矢量 - “法线”。

Three points; a, b, and c, in 3D coordinate space form the corner points of a triangle. Vector subtraction of b and c from a gives the long sides of the triangle, side 1 from a to b, and side 2 from a to b. The cross product of the vectors side 1 and side 2 gives a new vector perpendicular to the plane on which the three original points lie.
Vector3 a;Vector3 b;Vector3 c;Vector3 side1 = b - a;Vector3 side2 = c - a;Vector3 normal = Vector3.Cross(side1, side2);

A left hand demonstrates the left hand rule for determining the input order of vectors for calculating a cross product. The forefinger and thumb are extended at right angles to one another while the middle finger stretches out in a third plane at right angles to the forefinger. The forefinger and thumb represent the first and second input vectors to the cross product calculation respectively. The middle finger represents the resulting normal.
float perpLength = perp.magnitude;perp /= perpLength;
perpLength