Go Determination of the relative orientation of the target in Unity3d

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.