Why can't I use "equals" or "not equal" as a condition on a float

Hello everybody,

I just got started with animator and I got into a lil problem here.
I started a 2D Jump n run engine.

The char has animations for jump, walk and stand.
I pass two float Parameters to my AnimationController

xVelocity
yVelocity

When xVelocity is != 0 I want the Player to have the walking animation
When yVelocity is < 0 or > 0 (you could say “does not equal 0”) I want the jump animation to play.

The problem is …I can not use a “Equals” or “Not Equal” condition on float.
Is my approach wrong? …I don’t quite get why I can not check if a float is equal / not equal to some value…however…with INT I can check this :frowning:

Thanks in advance

Change your condition like this

for the transition idle to walk, create 2 condition
xVelocity Greater 0.1
xVelocity Less -0.1

for jump it almost the same thing too
yVelocity Greater 0.1
xVelocity Less -0.1

There is no ‘Equal’ or ‘Not equal’ because float equality is not well define on the floating point unit, you can always have rounding error that make you believe that the current value is 0 but in fact it close to zero which make the condition fail even if what you see in the editor is 0.

4 Likes

There IS a Mathf.Approximately() function though, so I’m not sure why Unity couldn’t use that in the background for an Equals comparator in the animation, rather than make us have to do some jank like this~

(Yes, I know this is a DEEP necro, but this thread is the first thing that pops up when you search for this issue on Google)

2 Likes

A mild necro on a DEEP necro: converting the floats to bools based on their absolute value should work more cleanly for this:

animator.SetBool("HasXVelocity", Mathf.Abs(xVelocity) > 0f);
animator.SetBool("HasYVelocity", Mathf.Abs(yVelocity) > 0f);
1 Like

Better yet:

animator.SetBool(“HasXVelocity”, xVelocity != 0);

Though the whole idea of arbitrarily dividing core game logic between scripts and a janky visual interface always seemed extremely foolish to me.

Mathf.Approximately() almost never works reliably, for floats you have to define your own tolerances and use those to compare for equality