- Home /
Any difference between using 0.1 and 0 for my conditions in my animator transitions?
I noticed that in most Unity tutorials, they use transitions with conditions such as "Less than 0.1" or "More than 0.1". Why is that? Wouldn't 0 be preferable? Or is it to make the transitions seem less stiff/robotic?
Sorry if it's a stupid question.
dude HOW $$anonymous$$ANY FRA$$anonymous$$ES in your animation ?
say it has 4 frames.
so frame [2] for instance is um 0.500 until 0.7500
note that if you try ANY NU$$anonymous$$BER BETWEEN THERE, you're still really just getting frame 2
(with some smoothing, but that's another issue)
it could be this is what's tripping you up
Answer by Lockstep · Apr 07, 2013 at 12:46 AM
Well in real life maths any number can be memorized exactly. But the computer can't do this. It only has finit memory to save a number. The computer has two basic types of numbers it can save: integers and floats.
Integers are numbers {..., -1, 0, 1, 2, ...}. Integers are exact thus you can compare them with an equal sign ( == ). But due to the limited memory there is a maximum which the integer can't exeed. This means a 16 bit integer can actually be out of this set: {-32.768, ..., +32.767}
Floats on the other hand are numbers like 1.234567E+20. The computer saves the first couple of numbers (7 for the 32 bit float) and an exponent. The biggest (ignoring the sign) float possible is 1.79E+308 and the smallest is 1E-44. A far bigger range than the integer. The downside is, that floats are not exact. You usually have a hard time comparing two floats with the == operator. Instead check if the difference between two floats is close to zero.
Enough with the general talk. Back to your question. If you want to, lets say, animate a person. You have a running animation and an idle animation. Of course you want the running animation to play if the character runs and the idle animation otherwise. If you would check velocity.magnitude == 0 as condition, then the running animation would be played even if velocity.magnitude would be 2.463E-7. The character would run happily on the spot. Not good.
Short answer: Getting a float to an exact value is not always possible. You should never use == to compare a float since floats are not exact and this could lead to unwanted effects (see the example above). To compare to floats, sustract them and see if the result is small / close to zero.
Should just point out that Unity's $$anonymous$$athf library contains a function called Approximately that can be used for comparing floats with specific values.
Very good answer! @whydoidoit do you know what is behind Approximately function? What are the rules for getting true (return value)? Documentation only says "if they are similar".
Your answer
Follow this Question
Related Questions
Animator bug when activating a transition condition using SetBool 0 Answers
Animator dont transition to self or any sub state 2 Answers
Parameterize the Animators transition condition (in 2D) 1 Answer
Complex condition of animator transition via script 2 Answers
Animator problem with multiple sprites and one controller 0 Answers