[Unity] Dot dot product & Cross cross product

Dot dot product

Dot (Vector3 lhs, Vector3 rhs)

Definition: a·b=|a|·|b|cos<a,b> The
dot product is the multiplication of two vectors and then multiplied by the cotangent function between the two vectors.
For normalized vectors, Dot returns 1 if they point in exactly the same direction, -1 if they point in exactly opposite directions, and 0 if the vectors are perpendicular to each other.

scenes to be used:

  • You can use Dot to find out whether another object is in front of the current object
        Vector3 A = transform.forward;
        Vector3 B = go1.transform.position - transform.position;
        float red = Vector3.Dot(A, B);
        
        if (red>0) {
    
    
            Debug.Log("这个物体在物体前方");
        }else if (red == 0) {
    
    
            Debug.Log("这两个物体方向垂直");
        }else {
    
    
            Debug.Log("这个个物体不在玩家前方");
        }

Cross cross product

Cross (Vector3 lhs, Vector3 rhs)
definition: c = axb, where abc is a vector. The
cross product returns a vector that is perpendicular to the two input vectors.

Summary:
Before and after dot product judgment, return 1 means it is in the front, -1 means it is in the back, and 0 means it is exactly vertical.
The cross product judges left and right, returns 1 for the right side, and -1 for the left side.

Guess you like

Origin blog.csdn.net/ainklg/article/details/129760154