Saturday, November 5, 2022
HomeGame Developmentunity - Animation performs when strolling left however not proper

unity – Animation performs when strolling left however not proper


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:

  1. We hit the primary if and move the check, 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 hooked up else if or else blocks to skip over.

  4. We hit the second if and fail the check, 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 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 ifelse ifelse 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;
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments