Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Idle animation gets played when changing walk directions (CODE)

Discussion in 'Animation' started by GuirieSanchez, Jul 25, 2022.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    406
    DISCLAIMER: I'm implementing the animation system with code, I do NOT want to use mecanim.

    Well, the issue I'm having is that if I'm going leftwards (the input is -1) and the walk animation plays, but as soon as I move the joystick to the other side to go rightwards, then the input goes all the way from -1 to 1, and because of it, at some point in the transition, the 0 input is detected, and the idle animation plays (and it feels really awkward and wrong).

    this is a sample code that would make the transition:
    Code (CSharp):
    1. if (input.x == 0) // or if(velocity.x == 0)
    2.         {
    3.             player.anim.crossfade("idle", 0, -1, 0f, 0f);
    4.         }
    5.         else
    6.         {
    7.             player.anim.crossfade("move", 0, -1, 0f, 0f);
    8.         }
    Any idea on how to solve this issue?

    Thanks for reading.
     
  2. A1Qicks

    A1Qicks

    Joined:
    Dec 11, 2021
    Posts:
    24
    I'm incredibly new to coding, so take my suggestion with a pinch of salt.

    But couldn't you count frames to see how long input.x equals 0, and only implement the crossfade if it's longer than 1-2 frames? I'm assuming it passes through 0 more or less instantly.
     
    GuirieSanchez likes this.
  3. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    406
    If you're pixel perfect it's 16 frames (but obviously this is frame-dependent). The problem with the crossfade is that it should be applied to the running animation (so that it stays running while you change directions). You only want to apply the crossfade only if you're changing to the idle animation, but not to jumping, falling, attacking, etc.; thus you would probably have to add conditions and dirt the code, and I want to avoid that as much as possible.

    I came to realize that it's normal that the idle animation gets played. In fact, I believe that either you use velocity or input to detect the animation change, everyone's character (in a 2d platform) will have the idle animation playing when changing directions and I'd bet they didn't even notice it (and this not necessarily a bad thing, it's just how it should behave by the nature of that change of directions and the animation transition setup).

    If you want to notice this transition, you can replace the idle animation with a black square that covers the whole screen, and you will see the black screen popping up all the time whenever you change directions.

    An alternative to using a crossfade for the running animation that I came up with would be to use an animation transition that you can call, for instance, "turning". You also have to set up parameters to check if your player is facing Left or Right:
    If the player is running leftwards and suddenly stops, the idle animation (or the stop animation) will fire off automatically because when the player was running the
    facingLeft bool
    was true, and when it stopped, the
    facingLeft bool
    remained true. However, if the player is running leftwards and suddenly changes direction, then the "idle state" will ask the facing state of the player, and if it detects a change, it will fire off instead the "turning" animation. Once the turning animation is done playing it will fire off the running or the idle animation depending on the input.​
     
    Last edited: Jul 30, 2022
  4. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    406
    Now that I think about it, this doesn't look like it works, because, in order to change the facing bool of the player, you need to pass through 0 input (therefore triggering the idle animation).
     
  5. KaduYogesh99

    KaduYogesh99

    Joined:
    Jun 5, 2020
    Posts:
    3
    it's easier if you where using new input system and separate idle and other movement animation's like
    idle transition to locomotion blend tree, like that you can check for input actions state(in new input system) weather its ended or not,
    if its ended and input vector magnitude is 0 || < 0.2f then transition to idle. ;)

    but depends on your preference.
    let me know if it works
     
    GuirieSanchez likes this.
  6. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    406
    I'm using the new input system, but I have no idea about blend trees (I only know, for instance, that it's normally used in animations for top-down RPGs because of the multiple directions) (mine is a 2d platformer).
    Also, the way I'm implementing the logic for the animations is by using a state machine that manages the logic of the state transitions (and triggers the animations). A super basic example: if velocity = 0 then switches to IdleState (and triggers the Idle animation), else if velocity != 0, then RunState (run animation). Do you think this setup is ok for locomotion blend trees?

    Anyway, I'll dig into it and see what I can do. Thank you for the suggestion.
     
  7. KaduYogesh99

    KaduYogesh99

    Joined:
    Jun 5, 2020
    Posts:
    3
    so if you can add another variable which keep tract of joystick inactive state (no input given to stick), then move to idle
    just an example:
    if(InactiveStick){
    move_idle
    }
    else{
    magnitude > 0 ? move_right : move_left
    }


    you can implement same conditions in animatorController.
    (that's what i think;))