Vector2 - Getting different values when printing vector and vector.y

Unity Ver: 2021.3.2f1

Hello,

Vectors aren’t my strong point, and I’m confused by this result I’m seeing.

I have the following code:

currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
print("currentDir: " + currentDir);
print("currentDir: " + currentDir.y);

As you can see, I have two print statements, one which prints the vector and one which prints the .y component of the vector. I would expect these values to match, but what I’m seeing is this:

Surely these outputs should be the same?

Not necessarily. The first calls ToString() of Vector2 and the second version calls ToString() of float. I believe the former is an override by Unity which pretty-prints the values (eg only two digits following the decimal point etc). Set a breakpoint and check the debugger to confirm that both values are actually the same.

Whoops, sorry for the late reply @CodeSmile . When I breakpoint it, this is what I see:

Again, we can see the Y is a crazy number, but the normalised vector is 0.00.

Is that normal?

That’s not a crazy number it’s scientific notation for a very small number, which when truncates to only two digits is displayed as 0.00.

3 Likes

@Neto_Kokku Thanks! I still maintain though that all numbers are crazy.

It is also called E notation and it keeps throwing me off every time … :face_with_spiral_eyes:

2.694422E-22 is actually a number with 22 zeros in front of it, like so:

0.0000000000000000000002694422

In my book that is equal to 0 and the computer shouldn’t be trying to drive home a point I don’t give the slightest damn about. :smile:

1 Like