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.
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).