I’d like to share some knowledge about the pitfalls and neat tricks I got to discover under the form of short, easily-readable blog posts.
Today’s topic : how to play a random animation from a predefined list.
I’m currently researching how to work with the animation blueprint system of UE4, and it takes a lot of trial and error to test out some simple and more complex use cases.
While doing this, I realized some of the things I assumed would be braindead easy to do in Unreal turned out to be very convoluted, so to speak.
The Animation Blueprint is one of those tools that seem to me to be good at two things :
- extremely simple use cases
- extremely specialized use cases
So there is a whole galaxy of « kinda common use cases » in the middle that the tool doesn’t always address in a very straightforward way.
In my experience, Unity Animator, in comparison, feels much more « to the point » as it lets you « get it done » faster. But I digress.
Playing a random death animation
My use case was a very simple one.
As you may know, the mannequin provided by the UE Animation Starter Pack has three different « default » death animations (Death_1 / 2 / 3).
So playing a death animation is indeed dead (ah !!) simple :
Add a Died boolean in the Animation BP, set it to true when your character dies, and in the anim graph, blend poses by bool to play a death animation when the boolean is true. Boom you’re done (and don’t forget to untick « Loop Animation » in the Details or your character is going to die again and again and again…).
What if we wanted to pick a random death animation between the three available ones ? This is where all hell breaks loose.
Most of the usual BP control flow nodes don’t work the same way in the Anim Graph. We’re manipulating animations here, not « control flow » per se. So a lot of your initial ideas to implement it Just Won’t Work™.
Plus, a lot of the advice you’ll get is to learn a bunch of complex animation tools like Animation Montages or other more or less shady animation BP tricks. Documentation is lacking at best, and a lot of useful information is buried in hour-long videos on Youtube you don’t care to watch.
Picking a random death animation shouldn’t be (or feel) that hard to do… What if we’re lazy and don’t want to deal with an anim montage or whatever « just for that » ?
After a lot of trial and error, I think I found a rather easy solution to work with that I’m satisfied about enough. It isn’t very flexible, but perfectly suits my current use case, so here goes :
- First go to your Animation BP event graph, and add the Event Blueprint Begin Play node if it’s not already there. You will need two new variables : Death Anim Number (which is the total number of different death anims you can pick) and Chosen Death Idx, which will be the index of the chosen one. Then just use a Random Integer in Range node to pick one at random.
The careful reader will think « Death Anim Number » is a bit of a misnomer (for a lack of a better name), and you would be right, because Random Integer in Range returns a number like this : Min <= number <= Max. Which means if you want to pick between 3 anims, Death Anim Number should actually be 2 (so it can pick 0, 1 or 2). Keep that in mind !
- Next, go to the Anim Graph. I’m not using the Locomotion state machine for this, as I actually stay very close to the dead easy scenario from before.
Right-click anywhere in the anim graph to create a Select node.
Select nodes have no type by default (a wildcard type) so that anything can be plugged to them. But how can we plug animations into a Select node ? We need to go through some steps, and I’ll assume here that we know in advance all the animations we want to select from.
- First of all, in our 3 death anims case, 2 options is not enough to choose between all of them, so right-click in the body of the node to select « Add Option Pin » . Repeat as much as you have animations to play.
- Then, you have to tell the Select node we actually want to select between animations. To do that, we’ll change the Pin Type to Anim Sequence. Because, at the end of the day, a single animation is just an anim sequence with only one animation in it ! Right-click on « Option 0 » for example to summon the « Pin Actions » context menu.
- Once this is done, the Select Node will actually let you select animation assets. This is where you put all the animations you want to be able to play at random.
- We’re almost done ! But we now have to find a way to make the selected sequence play. My idea was to just put a regular Play node, then go to details and expose its Sequence pin, so that we can connect the Select return value to the Play node (that should change its title to « Play Animation Sequence » ) :
- Finally, all that is left to do is plug your chosen death idx to the Select Node, and plug the Play Animation Sequence node to the Blend poses by bool just like our very first example :
Works like a charm ! Every instance of this animation blueprint will then choose a random index on begin play and then play the matching animation on death. I am very aware this is kind of a hack (what if we had thousands of them ?) and not very flexible, but it suits my simple use case very well.
Let me know in comments if you would have done it differently 🙂