Original: wolf96
In the process of doing RPG games, often encountered to judge the surrounding monsters relative to their own position
1. The following method can be used to determine the target in its own direction:
Vector3.dot (Transform.forward, target.position-transform.position)
When the return value is negative, the target is in front of itself, and vice versa in its own rear
2. The following method can be used to determine the target in its own left and right direction:
Vector3.cross (Transform.forward, target.position-transform.position). Y
3. Here, incidentally, the dot product and cross product of space vectors:
A. dot Product
The dot product is calculated as: a b=|a| | b|cos<a,b> where |a| and |b| indicate that the modulo,<a,b> of the vector represents the angle of the two vectors. In addition, the,<a,b> and <b,a> angles in the dot product are not sorted.
So by dot product, we can actually calculate the angle of two vectors.
In addition, by calculating the dot product, we can easily determine whether the current object is facing another object: Just calculate the transform.forward vector of the current object and the dot product of the otherObj.transform.position, more than 0 in front, Otherwise in the rear.
B. Cross Product
Definition of cross product: C =a x B where a,b,c are vectors. That is, the cross product of two vectors gets the vector!
The nature of the 1:c⊥a,c⊥b, that is, the vector c perpendicular to the plane of the vector b.
Property 2: Die length |c|=|a| | B|sin<a,b>
Property 3: Satisfies the right hand rule. From this we have AXB≠BXA, and Axb =–bxa. So we can use the positive negative value of the cross product to determine the relative position of the vector A, B, that is, the vector A is in the clockwise or counterclockwise direction
Go Determination of the relative orientation of the target in Unity3d