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 antx · Feb 14, 2013 at 06:56 PM · vector3directioncomparerounding

How precise is unity with it´s transforms?

I need to check whether 2 objects have the same direction (are parallel). This is what I do:

 Vector3 myForward = transform.TransformDirection(Vector3.forward);
 Vector3 otherForward = otherCollider.transform.TransformDirection(Vector3.forward);
 if (Vector3.Dot(myForward, otherForward) == 1)
 {
     Debug.Log ("we are aligned (rotation)");
 }
 else
 {
     Debug.Log ("we are NOT aligned (rotation)" + Vector3.Dot(myForward, otherForward));
 }

Now I placed two object in the scene with rotation 0,0,0 in the inspector. (also scale 0,0,0 for both and position 0,0,0 for one and 0,0.45,0 for the other). This gives me the debug line "we are aligned (rotation)". So it worked.

Then I chance the rotation of both object in the inspector to 315,0,0 and this gives me the debug line "we are NOT aligned (rotation)0.9999999". So it did not work.

What is going on? Are they truely not facing the same direction or should I use some rounding function on the result? Or Am I doing something completely wrong here?

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
2
Best Answer

Answer by GerryM · Feb 14, 2013 at 07:30 PM

This is basically a float precision problem. Never compare two floats (the return of the dot function) and your 1 without considering some internal rounding errors. Try something like:

 if (Math.Abs(Vector3.Dot(myForward, otherForward) - 1f) < 0.0001f)

Furthermore, rotation data is internally stored as quaternions, what you see in the inspector (vector3) therefore has a further conversion error.

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 antx · Feb 14, 2013 at 10:02 PM 0
Share

Thanks to the two of you. I´m going to round then to 3 or 4 decimal places.

avatar image
1

Answer by Eric5h5 · Feb 14, 2013 at 07:30 PM

You should round at least a little, because of floating point imprecision. By the way, you can just say transform.forward instead of transform.TransformDirection(Vector3.forward).

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

11 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

Related Questions

Transform angle to grid position 2 Answers

Snap a direction vector 1 Answer

Local FPS Gravity Changing Lerp 0 Answers

How do I turn 1 objects rotation into another objects movement direction? 1 Answer

Finding difference between gameObject's x coordinate according to one object's transform 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