Overview
By the end of this chapter, you will be able to use the Animation Montage tool to create a unique throwing animation using the Throw animation sequence you imported in Chapter 10, Creating a SuperSideScroller Game. With this montage, you will create and use Anim Slots that will allow you to blend animations in the Animation Blueprint for the player character. You will also get to know how to use blending nodes to effectively blend the movement and throwing animations of the character.
After finalizing the player character animation, you will create the required class and assets for the enemy AI and learn more about Materials and Material Instances, which will give this enemy a unique visual color so that it can be differentiated in-game. Finally, the enemy will be ready for Chapter 13, Enemy Artificial Intelligence, where you will begin to create the AI behavior logic.
In the last chapter, you were able to bring the player character to life by implementing movement animations in a Blend Space and using that Blend Space in an Animation Blueprint to drive the animations based on the player's speed. You were then able to implement functionality in C++ based on player input to allow the character to sprint. Lastly, you took advantage of the animation state machine built-in Animation Blueprints to drive the character movement state and jumping states to allow fluid transitions between walking and jumping.
With the player character Animation Blueprint and state machine working, it's time to introduce Animation Montages and Anim Slots by implementing the character's Throw animation. In this chapter, you will learn more about animation blending, see how Unreal Engine handles the blending of multiple animations by creating an Animation Montage, and work with a new Anim Slot for the player's throwing animation. From there, you will use the Anim Slot in the player's Animation Blueprint by implementing new functions such as Save Cached Pose and Layered blend per bone so that the player can correctly blend the movement animations you handled in the previous chapter with the new throwing animation you will implement in this chapter.
Let's start by learning about what Animation Montages and Anim Slots are and how they can be used for character animation.
Animation blending is the process of transitioning between multiple animations on a skeletal mesh as seamlessly as possible. You are already familiar with the techniques of animation blending because you created a Blend Spaces asset for the player character in Chapter 11, Blend Spaces 1D, Key Bindings, and State Machines. In this Blend Space, the character smoothly blends between the Idle, Walking, and Running animations. You will now extend this knowledge by exploring and implementing new additive techniques to combine the movement animations of the character with a throwing animation. Through the use of an Anim Slot, you will send the throwing animation to a set of upper body bones, and its children's bones, to allow movement and throwing animations to apply at the same time without negatively impacting the other. But first, let's talk more about Animation Montages.
Animation Montages are a very powerful asset that allows you to combine multiple animations and split these combined animations into what is called Sections. Sections can then be played back individually, in a specific sequence, or even looped.
Animation Montages are also useful because you can control animations through montages from Blueprints or C++; this means you can call logic, update variables, replicate data, and so on based on the animation section being played, or if any Notifies are called within the montage. In C++, there is the UAnimInstance object, which you can use to call functions such as UAnimInstance::Montage_Play, which allows you to access and play montages from C++.
Note
This method will be used in Chapter 14, Spawning the Player Projectile, when you begin to add polish to the game. More information about how animations and Notifies are handled by Unreal Engine 4 in C++ can be found at https://docs.unrealengine.com/en-US/API/Runtime/Engine/Animation/AnimNotifies/UAnimNotifyState/index.html.
You will learn more about Notifies in the first exercise of this chapter, and you will code your own notify state in Chapter 14, Spawning the Player Projectile.
The image below shows the Persona editor for Animation Montages. However, this will be broken down even further in Exercise 12.01, Setting Up the Animation Montage:
Just like in Animation Sequences, Animation Montages allow Notifies to be triggered along the timeline of a section of an animation, which can then trigger sounds, particle effects, and events. Event Notifies will allow us to call logic from Blueprint or C++. Epic Games provides an example in their documentation of a weapon reload Animation Montage that is split between animations for reload start, reload loop, and reload complete. By splitting these animations and applying Notifies for sounds and events, developers have complete control over how long the reload loop will play based on internal variables, and control over any additional sounds or effects to play during the course of the animation.
Lastly, Animation Montages support what are called Anim Slots. Anim Slots allow you to categorize an animation, or a set of animations, that can later be referenced in Animation Blueprints to allow unique blending behavior based on the slot. This means that you can define an Anim Slot that can later be used in Animation Blueprints to allow animations using this slot to blend on top of the base movement animations in any way you want; in our case, only affecting the upper body of the player character and not the lower body.
Let's begin by creating the Animation Montage for the player character's Throw animation in the first exercise.
One of the last things you need to do for the player character is to set up the Anim Slot that will separately categorize this animation as an upper body animation. You will use this Anim Slot in conjunction with blending functions in the Animation Blueprint to allow the player character to throw a projectile, while still correctly animating the lower body while moving and jumping.
By the end of this exercise, the player character will be able to play the Throw animation only with their upper body, while their lower body will still use the movement animation that you defined in the previous chapter.
Let's begin by creating the Animation Montage for the character, throwing and setting up the Anim Slot there:
When you open the Animation Montage asset, you are presented with a similar editor layout, as you would when opening an Animation Sequence. There is a Preview window that shows the main character skeleton in the default T pose, but once you add animations to this montage, the skeleton will update to reflect those changes.
With this exercise complete, you have successfully created an Animation Montage asset for the Super SideScroller project. Now it is time to learn more about Animation Montages and how you can add the Throw animation and Anim Slot you need in order to blend the Throw animation with the existing character movement animations.
Have a look at the following figure:
Underneath the Preview window, you have the main montage timeline, in addition to other sections; let's evaluate these sections from top to bottom:
For the purposes of the throw montage, you do not need to use this feature since you will only be using one animation in this montage:
Now that you are familiar with the interface for Animation Montages, you can add the Throw animation to the montage by following the next exercise.
Now that you have a better understanding of what Animation Montages are and how these assets work, it is time to add the Throw animation to the montage you created in Exercise 12.01, Setting Up the Animation Montage. Although you will only be adding one animation to this montage, it is important to emphasize that you can add multiple unique animations to a montage that you can then play back. Now, let's start by adding the Throw animation you imported into the project in Chapter 10, Creating a SuperSideScroller Game:
In Asset Browser, find the Throw animation asset. Then, left-click and drag it onto the timeline under the Montage section:
Once an animation is added to the Animation Montage, the character skeleton in the Preview window will update to reflect this change and begin playing the animation:
Now that the Throw animation has been added to the Animation Montage, you can move on to create the Anim Slot.
The Anim Slot Manager tab should be docked next to the Asset Browser tab on the right-hand side. If you don't see the Anim Slot Manager tab, you can access it by navigating to the Window tab in the toolbar at the top of the Animation Montage editor window. There, left-click to select the option for Anim Slot Manager and the window will appear.
With this exercise complete, you have added the Throw animation to your new Animation Montage and you were able to play back the animation to preview how it looks in the editor through Persona.
Now, you can move on to learn more about Anim Slots and Anim Slot Manager before adding your own unique Anim Slot to use for animation blending later in this chapter.
Anim Slot Manager is where you, as the name suggests, manage your Anim Slots. From this tab, you can create new Groups, which allow greater organization of your slots. For example, you can create a Group by left-clicking on the Add Group option and labeling it Face to articulate to others that the slots within this group affect the face of the character. By default, Unreal Engine provides you with a Group called DefaultGroup and an Anim Slot called DefaultSlot that is in that group.
Let's create a new Anim Slot.
Now that you have a better understanding of Anim Slots and Anim Slot Manager, you can follow these steps to create a new Anim Slot, which you will call Upper Body. Once you have this new slot created, it can then be used and referenced in your Animation Blueprint to handle animation blending, which you will do in a later exercise.
Let's create the Anim Slot by doing the following:
With the Anim Slot created, you can now update the slot used for the Throw montage.
Note
After changing Anim Slot, you may notice that the player character stops animating and returns to the T pose. Don't worry – if this happens, just close the Animation Montage and reopen it. Once reopened, the character will play the Throw animation again.
With your Anim Slot created and in place in the Throw montage, it is now time for you to update the Animation Blueprint so that the player character is aware of this slot and animates correctly based on it.
With this exercise complete, you have created your first Anim Slot using Anim Slot Manager, available in the Animation Montage. With this slot in place, it can now be used and referenced in the player character Animation Blueprint to handle the animation blending required to blend the Throw animation and the movement animations you implemented in the previous chapter. Before you do this, you need to learn more about the Save Cached Pose node in Animation Blueprints.
There are cases when working with complex animations and characters requires you to reference a pose that is outputted by a state machine in more than one place. If you hadn't noticed already, the output pose from your Movement state machine cannot be connected to more than one other node. This is where the Save Cached Pose node comes in handy; it allows you to cache, or store, a pose that can then be referenced in multiple places at once. You will need to use this to set up the new Anim Slot for the upper body animation.
Let's get started.
To effectively blend the Throw animation, which uses the Upper Body Anim Slot you created in the previous exercise with the movement animations already in place for the player character, you need to be able to reference the Movement state machine in the Animation Blueprint. To do this, do the following to implement the Save Cached Pose node in the Animation Blueprint:
Note
All cached poses will show in the context-sensitive menu. Just make sure you select the cached pose with the name you gave it in Step 1.
You will notice now, after Step 4, that the main character will animate correctly and move as you expect after the last chapter. This proves that the caching of the Movement state machine is working. The image below shows the player character back in his Idle animation in the preview window of the Animation Blueprint.
Now that you have the caching of the Movement state machine working, you will use this cache to blend animations through the skeleton based on the Anim Slot you created:
With this exercise complete, you now have the ability to reference the cached Movement state machine pose anywhere you would like within the Animation Blueprint. With this accessibility in place, you can now use the cached pose to begin the blending between the cached movement pose and the Upper Body Anim Slot using a function called Layered blend per bone.
The node that you will use to blend animations here is Layered blend per bone. This node masks out a set of bones on the character skeleton for an animation to ignore those bones.
In the case of our player character and the Throw animation, you will mask out the lower body so that only the upper body animates. The goal is to be able to perform the throw and movement animations at the same time and have these animations blend together; otherwise, when you perform the throw, the movement animations would completely break.
The Layered blend per bone function allows us to blend the Throw animation with the movement animations you implemented in the previous chapter, and give you control over how much influence the Throw animation will have on the player character skeleton.
In this exercise, you will use the Layered blend per bone function to completely mask out the lower body of the character when playing the Throw animation so that it does not influence the character movement animation of the lower body.
Let's begin by adding the Layered blend per bone node and discuss its input parameters and its settings:
Figure 12.13 shows the Layered blend per bone node and its parameters.
Before you connect anything to this node, you will need to add a layer to its properties.
There are again two parameters here, namely the following:
Blending is propagated to all the children of a bone to stop blending on particular bones, add them to the array, and make their blend depth value 0. The final result is as follows:
Now it's time to use the Anim Slot you created earlier to filter only the animations using this slot through the Layered blend per bone node.
When changing the Slot Name property, the Slot node will update to represent this new name. The Slot node requires a source pose, which will again be a reference to the Movement state machine. This means that you need to create another Use Cached Pose node for the Movement Cache pose.
With the Anim Slot and the Layered blend per bone node in place within the main character's Animation Blueprint, you are finally done with the animation side of the main character.
Next, let's have a brief discussion about the importance of animation blending for the Throw animation and what the Throw animation will be used for, before you move on to Exercise 12.06, Previewing the Throw Animation, where you will preview the Throw animation in the game.
So far, you have put a lot of work into ensuring that the Throw animation blends correctly with the Movement animations that you set up in the Animation Blueprint in the previous chapter. The main reason behind this effort is to ensure the visual fidelity of the character when performing multiple animations at once. You will learn first-hand the visual consequences of incorrectly setting up animation blending in the exercises and activity ahead.
Getting back to the Throw animation, every modern video game implements animation blending in one form or another, so long as the art direction and the game mechanics require such a feature. An example of a modern game franchise that extraordinarily uses animations is the Uncharted series developed by Naughty Dog.
If you are unfamiliar with the franchise, you can watch the full gameplay of the latest installment here: https://www.youtube.com/watch?v=5evF_funE8A.
What the Uncharted series does very well is use thousands of animations and blending techniques to give the player character an incredible sense of realism, weight, and movement that feels really good while you play the game. Although the Super SideScroller project will not be anywhere as polished as this, you are learning the basics of what is needed to make incredible animations for video games:
In the previous exercise, you did a lot of work to allow animation blending between the player character's Movement animations and the Throw animation by using the Save Cached Pose and Layered blend per bone nodes. Perform the following steps to preview the Throw animation in-game and see the fruits of your labor:
With this event in place, you need a function that allows you to play an Animation Montage when the player uses the left mouse button to throw.
The Play Montage function requires two important inputs:
Let's first handle Skeletal Mesh Component.
The last thing to do now is to tell this function which montage to play. Luckily for you, there is only one montage that exists in this project: AM_Throw.
Notice now how you can walk and run at the same time as throwing, and each animation blends together so as to not interfere with one another:
Don't worry about any bugs you might see when using the left mouse button action repeatedly to play the Throw montage; these issues will be addressed when you implement the projectile that will be thrown in a later chapter for this project. For now, you just want to know that the work done on the Anim Slot and the Animation Blueprint give the desired result for animation blending.
Let's continue with the SuperSideScroller project by now creating the C++ class, the Blueprints, and the Materials necessary to set up the enemy for use in the next chapter.
With the player character animating correctly when moving and performing the Throw animation, it is time to talk about the enemy type that the SuperSideScroller game will feature. We will have a simple type of enemy.
This enemy will have a basic back-and-forth movement pattern and will not support any attacks; only by colliding with the player character will it be able to inflict damage.
In the next exercise, you will set up the base enemy class in C++ for the first enemy type and configure the enemy's Blueprint and Animation Blueprint in preparation for Chapter 13, Enemy Artificial Intelligence, where you will implement the AI of this enemy. For the sake of efficiency and time, you will use the assets already provided by Unreal Engine 4 in the SideScroller template for the enemy. This means you will be using the skeleton, skeletal mesh, animations, and the Animation Blueprint of the default mannequin asset. Let's begin by creating the first enemy class.
The goal of this exercise is to create a new enemy class from scratch and to have the enemy ready to use in Chapter 13, Enemy Artificial Intelligence, when you develop the AI. To start, create a new enemy class in C++ by following these steps:
When you create a new class, Unreal Engine will automatically open Visual Studio for you with the .cpp and .h files ready to go. For now, you will not make any changes to the code, so close Visual Studio.
Let's create the folder structure in the content browser for the enemy assets next.
Now that you have the Blueprint for the first enemy using the EnemyBase class as the parent class, it is time to handle the Animation Blueprint. You will use the default Animation Blueprint that is provided to you by Unreal Engine in the SideScroller template project. Follow the steps in the next exercise to create a duplicate of the existing Animation Blueprint and move it to the /Enemy/Blueprints directory.
In the previous exercise, you created a Blueprint for the first enemy using the EnemyBase class as the parent class. In this exercise, you will be working with the Animation blueprint.
The following steps will help you complete this exercise:
With the enemy Blueprint and Animation Blueprint created, it's time to update the enemy Blueprint to use the default Skeletal Mesh mannequin and the new Animation Blueprint duplicate.
The Transform settings will appear as follows:
The following figure shows the settings of the Mesh component so far. Please make sure your settings match what is displayed here:
The last thing to do here is to create a Material Instance of the mannequin's primary material so that this enemy can have a unique color that helps differentiate it from the other enemy type.
Let's begin by first learning more about Materials and Material Instances.
Before moving on to the next exercise, we need to first briefly discuss what Materials and Material Instances are before you can work with these assets and apply them to the new enemy character. Although this book is more focused on the technical aspects of game development using Unreal Engine 4, it is still important that you know, on a surface level, what Materials and Material Instances are and how they are used in video games.
Note
For more information about Materials, please refer to the following Epic Games documentation: https://docs.unrealengine.com/en-US/Engine/Rendering/Materials/index.html.
A Material is a type of asset that can be applied to a mesh that will then control how the mesh looks in-game. The Material editor gives you control over many parts of how the end visual result will look, including control over parameters such as Textures, Emissive, and Specular, among others. The following image shows the default mannequin skeletal mesh with the M_UE4Man_Body-applied Material asset:
A Material Instance is an extension of a Material where you do not have access or control over the base Material from which the Material Instance derives, but you do have control over the parameters that the creator of the Material exposes to you. Many parameters can be exposed to you to work with from inside Material Instances.
Unreal Engine provides us with an example of a Material Instance in the Side Scroller template project called M_UE4Man_ChestLogo, found in the /Mannequin/Character/Materials/ directory. The following image shows the set of exposed parameters given to the Material Instance based on the parent material, M_UE4Man_Body. The most important parameter to focus on is the Vector parameter, called BodyColor. You will use this parameter in the Material Instance you create in the next exercise to give the enemy a unique color:
Now that you have a basic understanding of what materials and material instances are, it is time to create your own Material Instance from the M_UE4ManBody asset. With this Material Instance, you will adjust the BodyColor parameter to give the enemy character a unique visual representation. Let's start by creating the new Material Instance.
The following steps will help you complete this exercise:
Create a new folder called Materials in the Enemy folder. Left-click and drag the Material Instance into the /Enemy/Materials directory to move the asset to this new folder:
With this exercise complete, you have now created a Material Instance and applied it to the enemy character so that it has a unique visual representation.
Let's conclude this chapter by moving on to a short activity that will help you better understand the blending of animations using the Layered blend per bone node that was used in the earlier exercises.
At the end of Exercise 12.06, Previewing the Throw Animation, you were able to blend the movement animations and the Throw animation so that they both could be played in tandem without negatively influencing each other. The result is the player character animating correctly when walking or running, while also performing the Throw animation on the upper body.
In this activity, you will experiment with the blend bias values and parameters of the Layered blend per bone node to have a better understanding of how animation blending works.
The following steps will help you complete the activity:
Note
Make sure to return this value to 1.0f after you are done so as not to affect the blending you set up in the previous exercise.
Note
After this activity, make sure to return the settings of the Layered blend per bone node to the values you set at the end of the first exercise to ensure no progress is lost in the character's animation.
The expected outputs are as follows:
Note
The solution to this activity can be found at: https://packt.live/338jEBx.
Before concluding this activity, please return the Layered blend per bone settings to the values you set at the end of Exercise 12.05, Blending Animation with the Upper Body Anim Slot. If you do not return these values back to their original settings, the animation results in upcoming exercises and activities in the next chapters will not be the same. You can either set back the original values manually or refer to the file with these settings at the following link: https://packt.live/2GKGMxM.
With this activity complete, you now have a stronger understanding of how animation blending works and how blending weighting can affect the influence of additive poses on base poses using the Layered blend per bone node.
Note
There are a lot of techniques for animation blending that you haven't used in this project, and it's strongly recommended that you research these techniques, starting with the documentation at https://docs.unrealengine.com/en-US/Engine/Animation/AnimationBlending/index.html.
With the enemy set up with the C++ class, Blueprint, and Material, you are ready to move on to the next chapter, where you will create the AI for this enemy by taking advantage of systems such as Behavior Trees in Unreal Engine 4.
From the exercises and activities of this chapter, you learned how to create an Animation Montage that allows the playing of animations. You also learned how to set up an Anim Slot within this montage to categorize it for the player character's upper body.
Next, you learned how to cache the output pose of a state machine by using the Use Cached Pose node so that this pose can be referenced in multiple instances for more complex Animation Blueprints. Then, by learning about the Layered blend per bone function, you were able to blend the base movement pose with the additive layer of the Throw animation by using the Anim Slot.
Lastly, you put together the base of the enemy by creating the C++ class, Blueprint, and other assets so that they will be ready for the next chapter. With the enemy ready, let's move on to creating the AI of the enemy so that it can interact with the player.
3.141.202.187