Changing from walk to crouch animation in a state machine?

I have a 3rd Person character configured similar to the StackBot course 3rd Person Character, and have added a sprint and crouch mechanic to the character. I managed to get the sprint animation to load in the state machine, as I just needed to change the current blend space that was used.

Crouch on the other hand, is giving me a headache. I managed to get it to work by calling the character and getting the capsule height, then toggling a variable in my animation blueprint. I’ve been told that that isn’t the best way to do it, as it could create performance issues.

So now I have it configured with an interface, but I can’t get it to work. Any help is appreciated.

Character BP Event Graph:

Character BP Interface Variable:
BP3rdPerson Crouch Variable Interface

Interface Details:

Animation BluePrint Event Graph:

Animation BluePrint Interface Variable:
ABP3rdPerson Crouch Variable Interface

State Machine in Animation BluePrint:

State Machine transition from Ground to Crouch:

First of all, if you are using the base UE “character” class (or inheriting from it) you should have a character movement component. This component already has its own crouching ability and it is linked to an “IsCrouched” bool that comes with the base UE character. This is usually what people use to set the crouch variable in their AnimBP.
-This assumes that you are “crouching” through the character movement checkbox and didn’t create your own version of crouch.

Second, instead of having the animBP fish variables out of the character, most of the time the character BP pushes the variables to the anim graph.
-I would recommend just grabbing your mesh in your character and using a “get anim instance” + cast node to set your AnimBP’s crouch variable.

The whole thing would look a bit like this:

And, it also occurs to me, that if you want to make it even “prettier” than that, you could put the anim instance/cast on the character’s begin play and make a variable out of it. Then you wouldn’t even need to use a cast node anymore whenever you wanted to change a variable in the AnimBP.

After 6+ hours of searching, watching tutorials, asking questions, etc, you have fixed my issue. Thank you!

The only reason I changed this, was because I was casting from the AnimBlueprint, which would run on every tick. Is this going to function the same way, or would this only cast when running the crouch input action? If not, could I hook it up to the crouch input action to only cast when it’s pressed/released?

The easiest way to stop spam-casting things is to make variables in event begin play. You can find the anim BP/use the cast node in begin play and then promote the blue pin off of the cast to a variable. Then, you will always have access to the animBP without having to cast. :slight_smile:

As for attaching it to the crouch function, that could work, but it would probably require a .01 delay node right before you set the crouch.

Also, while you are correct that it is “bad” to cast on tick, a single actor doing it, like the main character player, usually isn’t going to cause a huge issue. It is if you have a lot of actors spamming tick casts that it starts to be problematic.

And, now that I think about it, the main reason the character usually pushes the variables is so that you can use the same anim BP for more than one character actor. However, since every character in the universe is going to have an IsCrouching variable, you maybe actually could just set crouch in the anim BP by casting to the “character” parent class of all your characters. Either way works in this case.

If the main issue you have is spam cast, then on begin play make a variable. That works for both the character BP and the anim BP whichever way you want to go.

I see this is already going in the right direction but to further improve your setup you can avoid Tick pulling values from the character entirely. On BeginPlay you bind to delegates on the character using the animation blueprint. For example a delegate (which you create / aka Event Dispatcher) which you execute on the character when you crouch. Set up the animation blueprint to listen to this delegate and update when the character does. Now you avoid having to check if the character pointer is still valid and you avoid pulling values from the characters when they haven’t changed.

1 Like

That…is a great idea.

^OP, this is smarter, do this idea instead.

You’ll have to excuse me, I’m still learning alot here.

I found a video regarding Event Dispatchers, and from how he configured them, it looks like he has a call when the user hits tab, then a cast on event begin play for the actor.

If I were to do this in my animation, wouldn’t it do the same thing as before? If I put it in the event graph, I’m back to it calling on tick, aren’t I? Or should there be a different way to call it?

For event dispatcher you make it in your character BP and attach it to your crouch action. It would look something like this (I’m using 5.1, so you might have the previous input system if you are using older UE, but it is the same idea):

Also, when doing this, you are no longer getting the crouch variable off of the character movement. Instead you are just setting a bool yourself based on if you have pressed crouch or not. So, you need to add the bool onto the details of the event dispatcher like this:

CrouchInputs

And, then for it to work, you need to go to the begin play of the ANIM BP and attach the event dispatcher to the variable there:

AnimBPCrouch

Only a single cast and no event tick at all!

Good luck with your project. :slight_smile:

2 Likes

You both are absolutely awesome!

You’ve helped someone to learn something new, and didn’t discourage them from trying to learn more.

Thank you! I’ve learned more from this thread then from hours and hours of reading through forums/YouTube

I actually have another question, off of your reply explaining things, if that’s ok?

The CrouchRecieved event that you have, that’s a custom event, right? How did you get it to have the IsCrouch variable there?

If you pull the red pin out from the bind event and then make a custom from it, it usually just gives you the needed inputs.

It isn’t any different than any other custom event that you make and add a bool input to though. It grabs the name of the bool you used for the event dispatcher if you pull the pin directly off the bind is all.

I’m gonna have to bug you again. Reading through Unreal Documentation, but I am hitting snags.

So, to start, I have a new Event Dispatcher created on my Character BP. I have it set so on the Input Action for Crouch, it will call the new Event Dispatcher, and set the variable within to either True or False depending on if crouch was pushed or released.

Now, reading through documentation, it states that I need to bind the event to the one that was created on the animation blueprint, so that it can trigger and the animation can update.

I’ve never added a custom event before, so I might have messed that part up. Here is what the blueprint looks like for my new test for this. I’m unsure of what needs to be done to bind this to the custom event, which is referenced under the photo below.

But then I took another look at the code you posted. The Animation BP is what is binding the event, but what triggers the event? How does that code execute or update the variable?

I also can’t seem to access the variable from the Event Dispatcher on the animation blueprint. I’d like to set it similar to how you have yours configured.

Okay, first of all, you can go ahead and plug in those call dispatcher events in your character BP. Those look correct. You don’t need that bind node in the character, so you can unhook and delete it.

The bind event in the Anim BP looks correct. I’m not sure why it is giving an error. You could try pulling the blue pin out from it again and regrabbing another version of the node and see if that fixes it.
-You should create a crouching variable in the anim BP itself if you don’t already have one. That’s the one you are going to set with the custom event.

You also asked about what triggers the event. Dispatchers work like this:
-The dispatcher exists on a blueprint somewhere. That blueprint can CALL the dispatcher whenever it wants.
-Other blueprints can BIND to the dispatcher. There is no limit to the number of blueprints that can bind to a dispatcher. The blueprint that owns dispatcher has no idea what all has been bound to the dispatcher and doesn’t interact with those other blueprints whatsoever.
-However, the blueprints that have bound themselves to the dispatcher very much know that the dispatcher exists and are listening for the CALL. Whenever the blueprint with the dispatcher calls the dispatcher, every single event bound to the dispatcher near and far off into the wild blue yonder goes off.

So, on the begin play of this anim BP, you are just BINDING the event to the dispatcher. The event is not being executed at this point in time. It will execute every single time the character blueprint CALLS the dispatch.

I hope that makes sense.

Edit: I noticed something strange in your picture of the character BP. The “input” in the details panel lists the bool as “IsCrouch,” but those call nodes in the picture have the bool a different name. You may have more than one dispatcher. Make sure you are calling the same one that you are binding in the Anim BP.
Edit 2: Oh, I think you just changed the name and haven’t compiled everything. Compile the character BP to fix up your dispatcher, then go to your anim BP, pull the blue pin, and make a new bind node.

How you explained that made it all finally click. Everything is working properly now!

Thank you for taking the time to explain that for me. Can I buy you a beer or a coffee or something? Seriously

1 Like

No problem.

I just have fun playing around in UE, but if you really want to “return the favor,” I now promote you to fellow forum helper. Go forth and help other poor souls get their games running!

1 Like

Absolutely! Anything that I can help with, I will

Cheers!