Just Wondering abt Mathf.abs > 0 and != 0

So i got this way of checking to see if the player is running or not, now i wanna create a way to check if the player is actually climbing or just hanging onto the ladder,

 if (isGrounded) // ground check
 {
     // Nếu trị tuyệt đối của vận tốc > Epsi thì chạy animation running
     if (Mathf.Abs(myRigidbody2D.velocity.x) >= Mathf.Epsilon)
     {
         myAnimator.SetBool("isRunning", true);
     }
     else // còn nếu không(đứng im) thì cho đứng im = cách tắt animation running đi 
     {
         myAnimator.SetBool("isRunning", false);
     }

 }

So ive been wondering, can i use myRigidbody2D.velocity.y != 0 to check the player velocity instead of abs and epsilon and all to simplify my code?

Hi,

The problem is that floats are ‘imprecise’. Furthermore, the physics simulation is constantly working on the player. Even if the player does not seem to move at all, it might be that the velocity is sometimes 0.000001f or something like that. 0.000001f is almost 0 but it is not == 0.

For this reason, Rick implemented a little threshold. He used Mathf.Epsilon but he could have used another value as well.

Did this make sense?


See also:

1 Like

What @Nina said is 100.000000001% (or 99.99999999999%) correct :grin:. Unity has a built in way that is a little more readable, though: Mathf.Approximately. This is to attempt to negate the floating point inaccuracies that may (and will) occur. The Debug.Log also truncates and approximates, so even if you Debug.Log a value and it shows it’s 0, it may still not actually be

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

Privacy & Terms