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 Nine · Nov 07, 2013 at 04:30 AM · vector3monodevelopdebugequal

If Expression True in Immediate Window But Code In if Block Never Runs

I set a break point in my code to break whenever I click on a surface. I then enter the immediate window and test to see if the the if statement will return true in comparing two Vector3's. It does return true. However, when I step over, the code is never run as though the statement evaluated false. Does anyone know how this could be possible? I've attached a screenshot.

Here is the picture of my debug window and immediate window. You can see where the immediate window evaluates to true. The second breakpoint is not hit.

alt text

Here is the details of the two Vector3's I am comparing.

alt text

Does anyone know why I am experiencing this? It really seems like an anomaly to me :/ Does it have something to do with threading?

untitled.png (162.8 kB)
2.png (182.5 kB)
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

1 Reply

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

Answer by Huacanacha · Nov 07, 2013 at 04:46 AM

Vector3's are made up of floats which makes comparing them directly for equality a bad idea. You only see to 1 decimal place precision for the Vectors in the debug windows but one Vector could be [0.0, 0.00001, -0.99999] and the other one [0.0, 0.0, -1.0]... you may consider them close enough to be equal but mathematically they are not.

This will work in your case:

 if (Vector3.Distance(hit.transform.up, upVectBefore) < 0.01)

Edit: on a closer look at the debug window, what I said is indeed correct... if you look at the individual x,y,z values they are different for each Vector. You need to decide what your tolerance level is for 'equality' and compare the vectors based on that (i.e. in the above code you need to decide to if distance should be less than 0.01 or 0.0001 etc, or alternatively look at the axis rotations and compare those within a certain tolerance).

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 Azrapse · Nov 07, 2013 at 06:00 AM 0
Share

To complement @Huacanacha's answer, have a look at $$anonymous$$athf.Approximately.

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

17 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

Related Questions

How do you attach the script debugger to iPhone? 3 Answers

Hitting Mono Breakpoint Crashes Unity 1 Answer

Monodevelop can't connect to iPhone for debugging 2 Answers

Monodevelop deleted my hierarchy/scene 2 Answers

What port does mono's debugger use 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