Struggling with animation...

I’m sorry, I know where are a ton of tutorials on this but at this point I’m beating my head against a wall trying to get this to work and I cant figure out why.

So I’ve imported my fbx model with two animations, one for idle and one for walking. I’ve created an animation controller, attached it to my player, and added two trigger which ive set in my script if any button is pressed or released to transition from walking to idle (because all I’m currently using are the directional buttons)… my animations play in the preview window, and when I look in the animation states I can see it successfully transitions from one to the other when I press and release the button, but my walking animation isnt playing and I have no idea why.

Ok I’m sorry but I’m still struggling with this, can anyone please help? I know this is a stupid question but if it takes someone smacking me on the head and telling me how stupid I am then please be my guest, I’ve exhausted my own resources at this point between tutorials and googling, no idea why I’m still having such a hard time with getting this to work. Thanks

You might be interested in Animancer (link in my signature) which is a much better way to play and control animations and has lots of detailed examples to explain how to use it.

1 Like

Sorry, I dont mean to just refuse help but I forgot to mention I’m using a platform specific version of 5.6 (which is also preventing me from posting code due to an NDA) and im not sure which features may or may not be supported. Also I do want to understand this (or else I’ll never learn any of it) and am not just looking for an end result, I don’t just want other people to do the work for me, sorry. Looking at what you did though I can see similarities in other places I’ve looked, just seem to be having some difficulty putting 2 and 2 together…

As of right now I’ve gotten my movement code working just fine, but when I try to make a either boolean values or triggers and write those into my code (then set them to the corresponding transitions in my animator window) it either changes to my walking state then won’t change back to idle or it won’t change to my walking state at all (depending how I structure my code or which animation parameter I try to use). And even when it does change to my walking state my animation doesnt play (despite playing in my preview window)… I see theres an animation.play() method but when I try to use it says it cant find my animation

Hi @CatDadJynx , is it possible to post screenshots of your animator’s structure (+ the conditions. on Trigger only and no exit time, that’s it?) + the code calling them ? What would be interested for us to see is how you set your animation parameters (like loop) and whether the bug is coming from the animator’s transitions or the code itself to guide you :wink:

Animancer can’t help you if you’re stuck back in Unity 5.6.

The Play method is pretty straightforward though. You make a state in the Animator Controller, then you can call animator.Play(“State Name”) to play it. If it says the state doesn’t exist then either you spelled it wrong or you have the wrong Animator Controller assigned to the Animator your script is controlling.

Hmm, alright. Honestly my guess would be it’s my script, though I’ve tried a few different things so at this point have probably just confused myself more. I’ll give it another look over and post screenshots better detailing where I’m at now. Thanks!

Alright, here is my relevent code… But at this point its kind of a mess because I have no idea what Im doing so just left it at my last attempt (between trying to comment out different lines trying to get it to work and/or trying to use bool instead of a trigger. The screenshot shows my animator window and one of my transitions (from walking to idle)- my idle to walking looks the same, only using the isWalking trigger. And laastly, on my Player game object Ive attached an animator component, where Ive made sure my animator controller is selected.

    Animator animator;

    public void Start()
    {
        animator = GetComponent<Animator>();
    }

    private void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
  
        Vector3 inputs = new Vector3(h, v);

        if (v != 0)
        {
            animator.SetTrigger("isWalking");
            animator.ResetTrigger("isIdle");
            animator.Play("playerWalking");
        }
        animator.SetTrigger("isIdle");
        animator.ResetTrigger("isWalking");
        animator.Play("playerIdle");
    }

Also at the moment Im getting this error regardless what I try:

'Player' AnimationEvent 'NewEvent' has no receiver! Are you missing a component?

You probably have an Event on your animation. Maybe you clicked on this button on your animation ?

6494497--730552--upload_2020-11-5_19-8-12.png

If so, Unity will want you to call an event function. Ex : If your event is called End_Animation, you have to have in a script (any script) a

void End_Animation()
{
   // some code
}

What I would suggest is not to use Trigger in your animator :wink: Use a single bool called Move (for ex).

In your script

  • if Move == true, your player transit sfrom Idle to Walk.
  • if Move == false, your player transits from Walk to Idle.

This would avoid resetting two triggers at a time.

So, your script should look like this (sorry, I’m writing without double-checking in a projet. Maybe I’ll get the spelling wrong) :

Animator animator;
    public void Start()
    {
        animator = GetComponent<Animator>();
    }
    private void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 inputs = new Vector3(h, v);
        if (v != 0)
        {
            animator.SetBool("Move", true);
        }
        else
       {
           animator.SetBool("Move", false");
       }
    }

Also, one note. No need to tell the animator to Play the state (animator.Play(“playerIdle”)). As you have an Idle and a Walk, they should be set to loop in their prefs.

At this point Ive tried all of that stuff before turning into the convoluted mess I posted above, but I cant seem to find the UI menu option for the evet trigger in the first screenshot you posted (to make sure I didnt click that button by mistake)… but I’ve been going through every single menu in the UI trying to find it for like 30 minutes now and still cant -_- after that I’ll put my code back to how it was with the changes you mentioned above (I’ve been getting this error for some time now so if I can find that UI option and make sure it’s off first I’m sure I can get it working otherwise).

Thanks for the detailed help by the way, I very much appreciate it (honestly just been struggling a bit with learning the not-so-intuitive UI)

Nevermind, I found it, sorry. And it seems that I did insert an event by mistake. Ill just try to rewrite my code real quick and see if I have any better luck. Thanks again!

1 Like

Nope, still no luck. Changed back to a boolean just like you put above. No errors anymore but also no state change to walking (though I have it set in my transition that it should)

Ah, sorry to spam the forum but now that I tried messing with it again my state change is suddenly working now it should be for some reason (?) But my animation still isn’t playing

Could you post a screenshot of your animations properties (select the animation and go to the inspector → Animation) ?

Also, if you are not moving your character with code and you want to use the displacement in your animation, you have to turn Apply Root Motion on on the Animator component :

6497836--731272--upload_2020-11-6_19-23-10.png

Also I am moving my character using code and root motion is currently unchecked. I also have an animator component attached to both my game object and player model (which is also a child of my player game object) and both are calling the same animator controller as well as player avatar- is this correct or could this be conflicting somehow? In my preview window for my walking animation (attached below) the animation plays correctly, and in my animator window I can see the states are changing correctly, but for some reason the two arent working together.

6498019--731311--screenshot.png


Hmm if I properly understand, you have :

  • Character (the one you dragged and drop in the scene)

  • A mesh

  • Your skeleton heirarchy

Correct?

You only need the Character to have the animator and the locomotion code. So only place both your Animator component and your locomotion script onto it. Don’t double the animator and the script.

That said, the issue can come from various reasons. You’ll solve your issue by eliminating one issue at a time.
First, have only one animator on your player and your code. You said that your code is correctly calling the animator’s bool and that’s great news !

Second, can you test your Walking animation stand alone.Test if in your animator as the default state and see if you get this blue bar progressing).
6498184--731341--upload_2020-11-6_21-20-44.png
If not, you have a problem with the animation itself.

6498184--731341--upload_2020-11-6_21-20-44.png

Thats what I initially thought but at some point got an error when I removed the animator from my model also (Im not sure why) but I just removed that and am still having the same issue. And thats why Im so confused, because my walking animation plays correctly in the preview window and I can see in my animator controller that the states are changed back and for between walking and idle how they should be, yet the walking animation never actually plays ( and my idle animation isnt even an animation, just a few still frames)… at some point my walking animation also played as the players default state (before I fixed it) so I know the animatioin plays correctly, just cant seem to figure out why it isnt now. Hmm… Also, yes, thats correct, here is my hierarchy.

6498238--731359--heirarchy.png

All right, seeing your hierarchy, your Animator and script should be put on PlayerRig and not Player. For the simple reason that it’s PlayerRig that contains the bones to move your skin mesh (Playermodel).

  • Do that and check if your animator is referencing the right animator etc
  • If you have errors (any errors), please linked them to the forum. That wil really let everyone help you :slight_smile: