Home Game Development unity – Animation performs when strolling left however not proper

unity – Animation performs when strolling left however not proper

0
unity – Animation performs when strolling left however not proper

[ad_1]

The downside could be extra apparent if we regulate 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 precise is totally separated from the case that checks to the left?

So let’s step by way of this code for a body when h > 0:

  1. We hit the primary if and cross the take a look at, so we enter the block.

  2. Inside this if block, "WalkParam" will get set to true.

  3. We resume execution on the finish of the primary if‘s block, because it has no connected else if or else blocks to skip over.

  4. We hit the second if and fail the take a look at, so we skip to the else block.

  5. Inside this else block, "WalkParam" will get set to false.

So we end this operate with WalkParam set to false.

You can repair this by altering your second if to an else if, so it is solely executed if the primary if take a look at fails, and skipped in any other case. That joins the total ifelse ifelse into one chain, the place precisely considered one of their contained blocks might be executed. Without this becoming a member of key phrase, every if might be checked impartial of the earlier take a look at’s consequence.

But you’ll be able to truly do all this way more merely:

// Use approximate comparisons for floats,
// in case of small rounding errors
if (Mathf.Approximately(h, 0)) {
    animator.SetBool("WalkParam", false);
} else {
    // Use one department for each stroll instructions.
    // Less repetition = fewer locations for bugs.
    float step = Mathf.Sign(h) * pace * Time.deltaTime;
    rework.Translate(step, 0, 0, Space.World);
    animator.SetBool("WalkParam", false);
    sr.flipX = h < 0;
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here