The issue may be extra apparent if we alter the whitespace in your code like so:
if (h > 0) {
animator.SetBool("WalkParam", true);
}
if (h < 0) {
animator.SetBool("WalkParam", true);
} else {
animator.SetBool("WalkParam", false);
}
See how the case that checks for motion to the proper is totally separated from the case that checks to the left?
So let’s step by means of this code for a body when h > 0
:
-
We hit the primary
if
and move the check, so we enter the block. -
Inside this
if
block,"WalkParam"
will get set totrue
. -
We resume execution on the finish of the primary
if
‘s block, because it has no hooked upelse if
orelse
blocks to skip over. -
We hit the second
if
and fail the check, so we skip to theelse
block. -
Inside this
else
block,"WalkParam"
will get set tofalse
.
So we end this operate with WalkParam
set to false.
You may repair this by altering your second if
to an else if
, so it is solely executed if the primary if
check fails, and skipped in any other case. That joins the total if
–else if
–else
into one chain, the place precisely one among their contained blocks shall be executed. With out this becoming a member of key phrase, every if
shall be checked unbiased of the earlier check’s final result.
However you may truly do all this far more merely:
// Use approximate comparisons for floats,
// in case of small rounding errors
if (Mathf.Roughly(h, 0)) {
animator.SetBool("WalkParam", false);
} else {
// Use one department for each stroll instructions.
// Much less repetition = fewer locations for bugs.
float step = Mathf.Signal(h) * pace * Time.deltaTime;
remodel.Translate(step, 0, 0, House.World);
animator.SetBool("WalkParam", false);
sr.flipX = h < 0;
}