Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AndrewGrayGames · Jun 25, 2015 at 03:21 AM · vector3.angle

How to determine if two Vector3s representing rotations are facing each other?

For a project I am working on, I have some rotation data in degrees stored as a Vector3 in an object. What I want to do is determine if two objects are facing each other.

The cases I'm evaluating look like this, in any given direction.:

 [R][1 ->][<- 2][R] 1 and 2 are facing each other.
 [<- 1][R][R][2 ->] 1 and 2 are not facing each other.

 [R][1 ->]          1 and 2 are not facing each other.
 [R]
 [2]
 [|]
 [V]

The code I've found that should return this relationship is:

 Vector3 normalized = (a - b).normalized;
 float indicator = Vector3.Dot(normalized, Vector3.forward);  // Is this the right axis?
 bool isFacing = indicator == -1.0f;

However, in the cases I've linked, I've had the following results:

 [R][1 ->][<- 2][R] Are 1 and 2 facing each other?  Answer: No (Indicator = 0)
 [<- 1][R][R][2 ->] Are 1 and 2 not facing each other? Answer: Yes (Indicator = 0)

 [R][1 ->]          Are 1 and 2 not facing each other? Answer: Yes (Indicator = 0)
 [R]
 [2]
 [|]
 [V]

Based on the indicator number alone, I can conclude something is off. So, some questions:

Question 1: Am I using the right algorithm to determine if two Vector3 angles are facing each other? Question 2: Assuming the answer to Question 1 is "Yes", why isn't my algorithm returning the expected results?

Finally, a caveat - I am not using Quaternion methods at all, because I can't unit test my code if it's present. This is for a procedural world generator, and I am writing unit tests A) because this sort of thing is difficult, and B) it's actually testable; this is a purely input-process-output sort of thing that is not real time, and thus would give seriously diminishing returns to QA by playtesting alone.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Jun 25, 2015 at 04:06 AM

If a and b are your direction vectors, your code should just be:

 float indicator = Vector3.Dot(a.normalized, b.normalized);       
 // Is this the right axis?
 bool isFacing = indicator == -1.0f;

(untested, written from my hotel room at Unite Europe where noisy neighbours have woken me up!)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maccabbe · Jun 25, 2015 at 04:44 AM 0
Share

Actually this would only indicate if the two objects are facing opposite directions. In both of the first two cases the objects are facing opposite directions yet Asvarduil wrote that he expects opposite results for these two cases so I think he wanted to know if two objects (with direction and positions) were looking at each other.

avatar image
0

Answer by maccabbe · Jun 25, 2015 at 04:38 AM

Vector3.Dot can be used to tell if one object is facing another. Just have one vector be the relative position of the other object and the other vector be the direction.

In case 1 one object is facing right and the other object is right of it

     Debug.Log(Vector3.Dot(Vector3.right, Vector3.right));

returns 1 signifying that there is 0 degrees between where the object is looking and where it should be looking meaning the object is looking at the other object.

In case 2 one object is facing right and the other object is left of it

     Debug.Log(Vector3.Dot(Vector3.right, Vector3.right));

returns -1 signifying that there is 180 degrees between where the object is looking and where it should be looking.

In case 3 one object is facing right and the other object is below it

     Debug.Log(Vector3.Dot(Vector3.right, Vector3.down));

returns 0 signifying that the is 90 degrees between where the object is looking and where it should be looking.

If you want to test if both objects are looking at the other then you'll have to test both. After all it is possible for the first object to be facing the second while the second object is not facing the first.

In your code you use (a-b).normalized as one of the vectors. I'll assume that this returns the direction vector of the other object. Then you use Vector3.forward as your other variable which I think is causing the problem. If the relative positions are right, left, down then forward is 90 degrees from all 3 which would explain why the indicator is 0 in all 3 cases. You probably want to use something like transform.forward (in this case the first object is always facing right so I used Vector3.right). And as I stated above, you'll have to check if the second object is also facing the first.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question about angles. 1 Answer

Movement in space - Scrip assistance 1 Answer

Finding the Angle for Enemy Ai 0 Answers

Vector3.Angle outputs different results for the same values 0 Answers

Vector3.Angle noob question 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges