Unity vector

The addition and subtraction of vectors will not be repeated in this article. This article focuses on the vector writing method in scripts.


1. Multiplication of vectors

Definition: k(x,y,z)=(kx,ky,kz)

If the length of the vector is L, and k takes 1/L, it can just make the length of the original vector become 1 and become a unit vector, which is called the normalization of the vector.

Because vectors of length 1 are very suitable for representing directions, vectors are often normalized. Here are a few examples:

Vector3 a=new Vector3(2,1,0);
Vector3 na=a/a.magnitude;   //a.magnitude是a的长度,术语叫做“a的模”
//以上写法等价于
Vector3 na2=a.normalized;
//
Vector3 na3=Vector3.Normalized(a);
//
Vector na4=(1/a.magnitude)*a;

Two common properties of the vector used in the above code: magnitude (mode) and normalized (standardized).

2. Dot product of vector

Definition: a b=|a| |b| cosα

(x1,y1,z1) ·  (x2,y2,z2)=x1x2+y1y2+z1z2

Physical meaning: projection distance

The result of dot multiplication is no longer a vector, but a number, which should be noted.

The dot product satisfies the commutative law, so it is the same whether a or b comes first.

The relevant scripts are described below

Vector3 a=new Vector3(2,1,0);
Vector3 b=new Vector3(3,0,0);
Vector3 dir_b=b.normalized;
float pa=Vector3.dot(a,dir_b);//dot是点乘函数

 3. Cross multiplication of vectors

Definition: | a × b|=|a||b| sinα

Different from the dot product, the result of the cross product is a new vector whose direction is perpendicular to the plane formed by the original two vectors. The specific result is judged by the left-hand rule (whether left-handed or right-handed is related to the coordinate system).

Left-hand rule: the palm is placed flat along the first vector, and it is full towards the second vector, and the pointing of the thumb is the cross product direction. As shown below

 Cross multiplication does not satisfy the commutative law, and if it is exchanged, the direction of cross multiplication will be reversed. i.e. a × b = - b × a

An important use of cross product in game development: to find the normal.

In game development, the front and back sides of the plane must be distinguished, so the normal is represented by a vector, and the direction of the normal is the direction of the plane. It is generally found that the length is fixed at 1 for calculation.

The normal can be easily obtained by cross product. For example, if the player is standing on a plane of the terrain and wants to get the normal of the plane, heuristic method needs to be used to obtain any two vectors on the plane. As long as the angle between the two vectors is not 0 or 180, take their fork The normal vector can be obtained by multiplying, and the mirror script is written as follows

Vector3 a=new Vector3(2,1,1);
Vector3 b=new Vector3(3,0,2);
Vector3 n=Vector3.Cross(a,b);//n为a,b平面的法线。Cross为叉乘
n=n.normalized;//将n标准化

4. Vector3

Vector3 belongs to struct (structure), this part gives the attributes, methods and operators of Vector3

                                                         Vector3 properties

field or property illustrate
x,y,z slightly
normalized get normalized vector (unit vector)
magnitude get the modulus of the vector, which is a scalar
sqrMagnitude Get the square of the modulus. The operation speed is faster than obtaining the modulus, because there is one less square root operation. Useful when comparing only two lengths without needing to find the exact length

                                                           Vector3 method

method illustrate
Cross

vector cross product

Dot vector dot product
Project() computes the projection of a vector onto another vector
Angle() Returns the angle between two vectors
Distance() Returns the distance between two vectors

The operators of Vector3 include +, -, *, /, ==, ! =.

5. Conversion of vector coordinate system

In Unity, you can use the transform.TransformPoint() method to convert local coordinates to world coordinates, or you can use the transform.InverseTransformPoint() method to convert world coordinates to local coordinates.

In fact, there are also vector coordinate system conversion functions, namely transform.TransformDirection() and transform.InverseTransformDirection().

Here is an example to illustrate how to change the direction of motion of an object through the global coordinate system and the local coordinate system:

1. Create a Cube, set the rotation Y to 300

2. Create a new script CoordinateLocal.cs, the content is as follows:

using UnityEngine;
public class CoordinateLocal:MonoBehaviour{
     void Update(){
     transform.Translate(Vector3.forward*Time.deltaTime);
     }
}

Hang the script on the Cube, run the game, and the object will move slowly along its own z-axis direction

3. Create a new script CoordinateWorld.cs, the content is as follows:

using UnityEngine;
public class CoordinateWorld:MonoBehaviour{
     void Update(){
     Vector3 v=transform.InverseTransformDirection(Vector3.forward);
     transform.Translate(v*Time.deltaTime);
     }
}

Hang the newly created script on the Cube, uncheck the original script, run the game, and find that the Cube has moved along the z-axis of the world coordinate system . Here's why:

The transform.Translate() function is based on the local coordinate system by default, so in script 1, although the parameter is Vetor3.forward, it will still be based on the front of the local coordinate system.

The 2nd script is a little more complicated. Since Translate is based on the local coordinate system by default, it is necessary to convert the forward of the world coordinate system into the vector v of the local coordinate system, and then use v as the parameter of Translate

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/130638843