Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Nition · Nov 23, 2012 at 01:03 AM · rotationforwardvectorsfacing

Detecting when objects are facing each other

I'm trying to detect when two objects are mirrored. I'm seen this question asked in the past and the best and simplest answer is checking if the dot product of the forward vectors is -1. Something like:

 Vector3 rotation1 = obj1.forward;
 Vector3 rotation2 = obj2.forward;
 float fotProd = Vector3.Dot(rotation1, rotation2);
 bool isMirrored = Mathf.Approximately(dotProd, -1);

Unfortunately this isn't working in all cases for me and now I'm getting confused about vector maths.

The problem is when two objects have rotations on the same axis only. For instance:

 Obj1.rotation.eulerAngles: x:0 y:0 z:90
 Obj1.rotation.eulerAngles: x:0 y:0 z:270

If I get the transform.forward for those, they're coming out the same: Both 0, 0, 1. Shouldn't it be different, because if you worked out which way they were actually facing, they'd be the opposite to each other?

Then of course when I do the dot product, it does 1 * 1 and gets 1, not zero. Any hints would be very appreciated.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Nition · Nov 23, 2012 at 03:56 AM

I've actually managed to solve my own problem, and for anyone in the future with the same issue I'll explain it here, because it took me ages to work out.

Any 3D rotation will have one axis which just rotates the object, but doesn't change the direction it's actually facing. Imagine a wheel spinning on a car - it's rotating but still facing the same way.

Now, normally in Unity this is a Z axis - hence my forward vectors were always coming out the same when I rotated Z. Unfortunately my objects were changing they way they faced when I rotated the Z, because I'd imported the models from Blender, and in Blender the Z axis is up instead of Y, meaning everything had a 90 degree rotation.

I decided to go into Blender and rotate my objects -90 degrees on the X axis, apply it, and then rotate 90 degrees and save. This then matches the objects visually between Blender and Unity but the 90-degree rotation that messed everything up doesn't have to be applied, and the problem is fixed.

I imagine the problem could also be fixed by multiplying the forward vector by your actual forward vector.

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
avatar image
4

Answer by eweeparker · Jun 18, 2015 at 04:44 AM

Debug.Log(Mathf.Abs(Vector3.Angle(transform.forward, target.transform.forward) - 180));

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 crare · Nov 26, 2017 at 11:22 AM 0
Share

I did something similar to your code. Is there way to know on which side is the other object? Because this only gives degrees between 0 to 180 on both sides.

avatar image
2

Answer by dre38w · Nov 23, 2012 at 01:46 AM

This seems a little messy but I whipped up something really quick that works well enough. I used Vector3.Angle.

   Vector3 lookDir = target.position - transform.position;
 
         Vector3 myDir = transform.forward;
         Vector3 yourDir = target.forward;
 
         float myAngle = Vector3.Angle(myDir, lookDir);
         float yourAngle = Vector3.Angle(yourDir, -lookDir);
 
         if (myAngle < 5.0f && yourAngle < 5.0f)
         {
             print("facing");
         }

If someone else has a cleaner way but at least it's a good starting point that works.

Comment
Add comment · Show 2 · 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 Nition · Nov 23, 2012 at 02:25 AM 0
Share

Thanks for that. I've actually managed to solve my own problem in a different way. See my answer.

avatar image dre38w · Nov 23, 2012 at 03:40 PM 0
Share

Nice. Good job then. haha And you're welcome.

avatar image
0

Answer by Jamalhaider92 · May 12, 2021 at 05:25 PM

I figured out this this var value = Mathf.Abs(obj1.transform.rotation.y - obj2.transform.rotation.y); this ranges from 0 to 1 if you rotate any of two objects if both objects are completely oposite to each other value will be 1 and in the case, object 1 and 2 are parallel value will be 0

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

13 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

Related Questions

GameObject facing fowards 1 Answer

Rotate vector A around vector B until it is perpendicular to vector C 2 Answers

Getting the face direction 2 Answers

Straighten out camera rotation 1 Answer

find the angle to rotate the gun to face mouse point with its barrel 1 Answer


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