Thy not use "if(myRigidbody.velocity.x != 0)"?

Is there any reason outside of teaching us to use all the Mathf stuff to not use:
if(myRigidbody.velocity.x != 0)

instead of:
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if(playerHasHorizontalSpeed)

?

Yes there is, comparing a float with 0f can give you unpredictable/unexpected results.

Hi TheGHA,

Michael is right. However, in our case, the problem is relatively predictable: Since we are dealing with the physics simulation (= something we cannot control), we cannot know if the velocity.x value will be exactly 0f. What if something makes the game object move with a velocity.x of 0.00001f? That’s basically “not moving” but also “not 0f”.

For this reason, we implemented a threshold to define when the player “is moving”. This does not have anything to do with the actual movement of the player. It’s just a definition. We define: If the player is slower than [insert value], we treat him as “not moving”.

Did this made sense?


See also:

1 Like

I see. Thanks for the answer.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms