Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Animator not playing animations

Discussion in 'Animation' started by spitre, Feb 15, 2018.

  1. spitre

    spitre

    Joined:
    Feb 15, 2018
    Posts:
    3
    I'm new to unity trying to set up a simple UI that makes a wolf 3D character sit. I set up a two state animator controller which transitions from the idle animation to the sitting animation when a bool goes from false to true. Then I attached the animator and a script to a game object. I put a button in a canvas somewhere else in the project and set the on click behavior to run a function in the script that changes the bool from false to true. I know the bool is being changed because I set up print statements to tell me what its state is and when I run the game the first animation plays but the character does not transition into the sit animation. My code is
    public void animationfunction()
    {
    anim = GetComponent<Animator>();
    if (anim.GetBool("SitBool") == false)
    {
    Debug.Log("false");
    anim.SetBool("SitBool", true);
    }
    else
    {
    Debug.Log("true");
    anim.SetBool("SitBool", false);
    }
    }
    Can anyone help with this?
     
  2. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    You do not need to use debug.log with animation transitions. You can simply open up the animator of the object while the game is running and view the bool values being cycled in the parameters tab. You can also view the state in which your animation is in. I recommend looking at that to determine where your animation is hanging up.

    I think your code is wrong though. You are not giving the animation time to transition before you set the bool SitBool back to false. SitBool is true for exactly one frame and false every other frame unless holding down the button and if you have it scripted for holding down. This means by the time Unity checks for the transition the bool may already be false again. You need to take out the else statement and check to see if the transisiton actually happened then set it back to false.

    Use this to get the current state.

    https://docs.unity3d.com/ScriptReference/AnimatorStateInfo.IsName.html

    Then once you know you are in the sit state set SitBool back to false.

    Also when posting code use insert>code key on the toolbar. It makes it easier to read.
     
  3. spitre

    spitre

    Joined:
    Feb 15, 2018
    Posts:
    3
    So I tried this
    Code (CSharp):
    1. public void animationfunction()
    2.     {
    3.         anim = GetComponent<Animator>();
    4.         anim.SetBool("SitBool", true);
    5.         if (anim.GetCurrentAnimatorStateInfo(0).IsName("Base.seat") == true)
    6.         {
    7.             anim.SetBool("SitBool", false);
    8.         }
    9.     }
    10. }
    Looking in the animator tab SitBool is turned to true when the button is pressed but the animation still doesn't play and SitBool is never turned to false. I guess this means either my code is incorrect(likely), for some reason setting sitBool to true doesn't transition the animator into the next state as it should, or both. Also, when SitBool is set to true, the animator displays the blue bar scrolling in the correct state indicating that its the playing the animation but the animation doesn't play. Any ideas?
     
  4. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    Take a screenshot of the animator controller playing with the blue bar you mentioned. If there is a blue bar then it is playing something, just maybe not what you think it is. Click on the animation state "sitting" in the animator controller and see what animation is in the Motion field in the inspector. It has to be that you have the wrong animation assigned to your animation state.

    https://docs.unity3d.com/Manual/class-State.html

    Also, have you looked at the preview window to see that your animation is in fact correct when you imported it?
     
  5. spitre

    spitre

    Joined:
    Feb 15, 2018
    Posts:
    3
    Sorry for the delay. The animation is correct, I checked the preview again. I've attached the screenshot of what it looks like when I hit play. It says its playing the animation I want when I want, that just isn't happening in the game
     

    Attached Files:

  6. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    I'm stumped at this point but lets break things down even more. Remove the transitions from the idle state and just add one entry transition to the sit state with no transition parameters. Do this and see if the animation goes immediately to the sit position on play. If it doesn't play then It will help determine what to do next. Go ahead and remove all scripts from that gameObject as well so we know that the scripts aren't overriding something.
     
  7. Super_Solomob422

    Super_Solomob422

    Joined:
    Jul 7, 2019
    Posts:
    1
    Same thing happens, to me. It works for the animation component but that won't loop it. And the animator component won't play the animation even thought the blue bar is there.
    .
     
  8. ThreeRing

    ThreeRing

    Joined:
    Aug 24, 2016
    Posts:
    11
    It has been a while since you post, but did you get a solution for this? I'm having the same issue and could use some tips!