What Unreal Engine 4 offers for animation in games

Animation in Unreal Engine 4 is mostly done in the Persona editor. This editor offers four different modes: Skeleton, Mesh, Animation, and Graph. These modes mainly exist so that you can jump straight into one of them to edit/create the animations more effectively. So, they are simply a loose group of functions that can be used to control the different aspects of animation. We will learn how to make use of the functions in Persona to add animation to our level.

To help improve team collaboration, Unreal Engine 4 also released a previously in-house-only toolset, which is a plugin for Maya (compatible for Maya 2013 and higher versions), known as Animation and Rigging Toolset (ART). This toolset provides a user interface to allow the creation of a skeleton, placement of the skeleton, and rig creation within Maya itself. We will not go into the details of this toolset, but you can find more information on this in Unreal's online documentation at https://docs.unrealengine.com/latest/INT/Engine/Content/Tools/MayaRiggingTool/index.html.

Importing animation from Maya/3ds Max

As many artists use Maya and 3ds Max to create 3D models and animation, Unreal Engine 4 has a great FBX Import pipeline that allows you to successfully import skeletal models, animation sequences, and morph targets. This makes it easy to transfer assets to the Unreal Editor and put them into the game. Unreal also tries to stabilize the import of art assets from other software, such as Blender and MODO.

Tutorial – importing the animation pack from Marketplace

Since 3D models and animation are first created outside Unreal Engine, for the purpose of learning about how animation works, we will import an animation pack that contains a 3D model with a number of animation sequences first, and we'll then learn how to make use of the different tools in the Unreal Editor for animation.

Unreal Engine offers a number of downloadable packs in Marketplace. Marketplace is in the start menu screen, which is under the Launch button. The following screenshot shows the startup screen that has the Marketplace tab selected for the downloadable packs. Search for Animation Starter Pack in Marketplace under Characters and Animations. This particular pack is free to download. Click on Animation Started Pack to download it.

Tutorial – importing the animation pack from Marketplace

After the pack is downloaded, you will find the pack added to the Library. The following screenshot shows where Animator Starter Pack is found in Library under Vault:

Tutorial – importing the animation pack from Marketplace

Now that we have the Animation Starter Pack in our Library, we can add it to our current project and start playing with the animations.

Click on Add To Project and a pop-up screen with all the current projects that are present in Unreal Engine will appear. Select the name of the project that you have been creating for all the various levels and all the tutorial examples. If you have followed the same project and level naming convention as me, it will be MyProject. I have also opened Chapter4Level from the previous chapter and renamed it Chapter5Level. The following screenshot shows AnimStarterPack loaded in the project:

Tutorial – importing the animation pack from Marketplace

What can you do with Persona?

Persona gives game developers the ability to playback and preview animation sequences, combine animation sequences into a single animation by blending, creating montages, editing skeletons/sockets, and controlling animation with Blueprints. I hope you still remember what you have learned about Blueprints in Chapter 3, Game Object – More and Move.

Tutorial – assigning existing animation to a Pawn

After adding the free animation pack into your project in the previous exercise, it is time to add some animation to the level. First of all, open Chapter4Level, rename it Chapter5Level, and then navigate to the AnimStarterPack folder using Content Browser. Go to the Character subfolder and click and drag HeroTPP into the level.

This screenshot shows how HeroTPP is added to the level:

Tutorial – assigning existing animation to a Pawn

The HeroTPP looks fake and frozen, right? Now, let's give him a better pose. Click on HeroTPP to display the details. Go to the Animation tab under Details and input the Animation Mode settings. Use Animation Asset, navigate and click on Jog_Fwd_Rifle in AnimStarterPack (in Content Browser), and then click on the arrow next to Anim to Play.

Tutorial – assigning existing animation to a Pawn

Here is a zoomed-in view of the Animation settings:

Tutorial – assigning existing animation to a Pawn

Now, build and play the level. You will see the character that you have just added to the level, is jogging.

This is the straightforward way to animate a character. However, the character continues to loop through this animation no matter what is happening around. We probably want the character to be able to react to the environment and conditions of the game. So, how can we do this?

Why do we need to blend animations?

In the previous exercise, we learned how to make a skeletal mesh take on a single animation. But can we make the skeletal mesh start running in a straight line? The next few sections of animation exercises will explain how we can do this and, subsequently, add more to this basic animation.

First of all, you need to remember that animation sequences/poses are played when you tell them to. While animating character, you need to look into the details so that the character looks normal.

Now, let's quickly recap what we did in the previous exercise: the skeletal mesh character was a zombie with no animation attached. When we linked the run animation and set it to play, the character immediately seemed like it was running. So, if we want the character to stop running, we can remove the run animation. The character goes back to looking like a zombie that hasn't been animated. If we did this in a game, you would probably think that there is something very wrong with the animation. Zombie->Running->Zombie. Nothing realistic about it.

How can we improve this? We start with an idle pose for the character; an idle pose is one where the character stands at a fixed spot and breathes. Breathing is part of animation too. It makes the character look like it's alive. Next, we set it to play the run animation. To stop this animation, we allow the character to take the idle position again. Not a bad attempt for this iteration. The character doesn't look like a zombie now, but it looks and feels real.

What else can we do to make this even better? Let's use an analogy of someone driving a car normally (not a race car driver). When moving from the start position, you accelerate from a speed of 0 up to a comfortable cruising speed. When you want to stop, you reduce the cruising speed by stepping on the brakes and then gradually go back to 0 (to avoid a stopping suddenly and giving your passengers the unpleasant experience of being thrown forward). Similarly, we can use this to help us design our character's transition from a stationary position. We will use a tool called Blend Animation to create this transition so that we can make the movement of the character a little more realistic.

Blend Animation, as the name suggests, blends various types of animation using variables. It can be a simple one-dimensional relationship where we use speed as an axis to blend the animations or a two-dimensional relationship where we use both speed and direction to blend animations. Unreal Engine's Blend Animation tool is capable of setting up the blending of animations in different ways.

Tutorial – creating a Blend Animation

In this example, we will use speed as the parameter to blend the animation. Let's quickly cover the thought process here first before listing the steps to follow in the Unreal Editor to achieve this. This would help in your understanding of how this process works instead of simply following the process to make something happen.

At speed = 0, we assign the idle pose. As the speed increases, we should switch the animation from an idle to a walking animation. As the speed increases even more, the animation switches from walking to jogging, and then running. Here's an illustration of how the blend would look:

Tutorial – creating a Blend Animation

Next, let's identify which animation sequences we have in the animation pack and would be suitable for each of the stages:

  • Idle_Rifle_Hip
  • Walk_Fwd_Rifle_Ironsights
  • Jog_Fwd_Rifle
  • Sprint_Fwd_Rifle

To create a simple 1D Blend Space, we can right-click on the Character folder, and go to Create Asset | Animation | Blend Space 1D. Alternatively, you can select the Character folder in Content Browser, click on the Create button at the top, go to Animation, and then Blend Space 1D.

Tutorial – creating a Blend Animation

Select HeroTPP_Skeleton; clicking on this creates a new Blend Space 1D. Rename newblendspace1d to WalkJogRun. Double-click on the newly created WalkJogRun to open the editor. This will propel you straight to the Animation tab of the editor. Notice that this part is highlighted in the following screenshot. In the SkeletonMesh field, we have HeroTPP_Skeleton, which was what we selected when creating the blend space earlier.

Tutorial – creating a Blend Animation

In the Animation editor, you have access to Asset Browser (which is, by default, in the bottom right-hand side of the screen). Clicking on the animation assets will allow you to preview how the animation looks.

Let's first set the X Axis Label to Speed. X Axis Range is from 0 to 375. Leave X Axis Divisions as 4.

The number of divisions creates segments in the speed graph that we have. Based on what we selected earlier for the Idle, Walk, Jog, and Run states, find the animation using Asset Browser, click and drop the animation into the WalkJogRun tab into the appropriate sections, as shown in the following screenshot:

Idle_Rifle_Hip is at speed = 0. Set Walk_Fwd_Rifle_Ironsights in the first division line. When you drag an animation into the graph, it creates a node and snaps at one of the division lines. Set Jog_Fwd_Rifle in the second division line and set Sprint_Fwd_Rifle at speed = 375. To preview how the animation blends, move the mouse over the graph along the vertical axis.

Tutorial – creating a Blend Animation

Tutorial – setting up the Animation Blueprint to use a Blend Animation

Now we have created a Blend Animation that uses speed as a parameter. How do we make an NPC change speed and then link this animation to it so that as the speed changes and the animation that is played also changes?

For a simple implementation of getting the speed and animation to change, we will set up the Animation Blueprint. Go to Content Browser. Navigate to Animation | Character; then, navigate and click on Create Asset | Animation | Animation Blueprint:

Tutorial – setting up the Animation Blueprint to use a Blend Animation

Upon selecting Animation Blueprint, the editor will prompt you about the base class that you want the Animation Blueprint to be created in. This screenshot shows the options that are available for selection:

Tutorial – setting up the Animation Blueprint to use a Blend Animation

In this example, we will pick the most basic generic class, AnimInstance, to build our Animation Blueprint in. Select HeroTPP_Skeleton as the target skeletal mesh for this blueprint. Name this Animation Blueprint MyNPC_Blueprint.

To check whether you have selected the correct target skeletal mesh, look in the Skeleton tab in the Blueprint window, as shown in the following screenshot. You should see HeroTPP_Skeleton in the box. The screenshot also shows the Graph tab that's been selected with the empty default AnimGraph showing. We will proceed through this exercise with the Graph tab selected, unless specified otherwise.

AnimGraph

This screenshot shows the default blank AnimGraph. Final Animation Pose will receive the output of the skeletal mesh that's been specified:

AnimGraph

First, we want to add a state machine by right-clicking within the AnimGraph and navigating to State Machines | Add New State Machine…, as shown in the following screenshot:

AnimGraph

Rename the newly created state machine Movement:

AnimGraph

Double-click on Movement. Create a new state named WalkJogRun:

AnimGraph

Double-click on the newly created WalkJogRun state to modify the state in a new tab. Go to the Asset Browser tab, look for WalkJogRun blendspace, which we created in the previous exercise, and click and drag it into the editor. Link WalkJogRun blendspace to the final animation, as shown in the following screenshot. Notice that speed = 0.00 is specified in the blendspace node; this was the variable that we defined to control the change of the animation when we created blendspace in the earlier exercise.

Next, we need to create a variable so that we can pass in a value to the WalkJogRun blendspace speed variable. To do so, we need to click and drag the green dot next to the Speed on the blendspace node to open up a contextual menu, look for Promote to Variable, and then click on it. This promotes speed in the blendspace node to a float variable, which we would set to control the speed and type of animation that will be played. Rename this new variable Speed. The following screenshot shows how we have created and connected a Speed variable to WalkJogRun blendspace, which is linked to Final Animation Pose:

AnimGraph

Now, go back to link Movement to Final Animation Pose:

AnimGraph

Now, the entire AnimGraph is linked up. Click on Compile, and you would see the preview of the character model updated, as shown in the following screenshot. The white moving dots show how data flows through the system. The speed is 0 here.

AnimGraph

We can also use this tab to see live preview as we change the value to Speed. The following screenshot shows you when speed is 50. The character model assumes a walking pose.

AnimGraph

Through AnimGraph, we were able to set up Speed as a variable and link this variable to WalkJogRun blendspace, which, in turn, controls what animation to play at which speed. We need to now think about how to provide some logic to determine how the speed of the NPC changes.

EventGraph

EventGraph is used to program logic into the Blueprint.

In this example, we will use EventGraph to create logic to change the speed values that will, in turn, affect the NPC's animation control.

To create a more complex intelligent decision-making process, which is termed as AI, we will need to use a set of AI-related nodes in EventGraph. We will learn more about creating AI in the next section.

The following screenshot shows the default new EventGraph tab in the Animation Blueprint.

The Event Blueprint Update Animation node can be thought of as the source that sends a pulse through the EventGraph network. As this pulse travels through the network, it goes through a series of questions that you design to determine which animation is played.

EventGraph

Try Get Pawn Owner is to get the owner that Animation Blueprint is assigned to. This is simply used in combination with another node, IsValid, to ensure that we have a valid owner before setting values to change the animation.

To make MyNPC_Blueprint work for the Hero_TPP mesh that we have in the level, we will need to first delete the Try Get Pawn Owner node and replace it with Get Owning Component. Right-click on the EventGraph and type Get. In the contextual menu that is opened, scroll down to find Get Owning Component. This screenshot shows where the Get Owning Component node is:

EventGraph

In the same way, right-click in the editor and type IsValid to look for the node. This screenshot shows where to get the IsValid node:

EventGraph

Now, link the triangular output from Event Blueprint Update Animation to the Exec input of the IsValid node (which is also a triangular input). Link Return Value (this has a blue circle next to it) output from Get Owning Component to Input Object (this has a blue circle next to it) of the IsValid node. The following screenshot shows the linkage of the three nodes.

The explanation for this is that at every tick, we need to check whether the target skeleton mesh is valid.

For now, let's simply set the speed of the NPC to 100 if the target skeleton mesh is valid. So, right-click on the EventGraph area, and type SetSpeed to filter the options. Click and select Set Speed, as shown in this screenshot:

EventGraph

Link the Is Valid output of the IsValid node to the input (this has a triangular symbol) of the SET Speed node. Then, click on the box next to Speed and type 100 to set the speed:

EventGraph

Save and recompile now to see how the preview model changes. The following screenshot shows the model playing the walk animation when speed is set to 100:

EventGraph

Now, Animation Blueprint is ready for use in the game level. We need to assign this Animation Blueprint to a character in the game. Save and close the Animation Blueprint editor to go back to the main editor.

To assign the Blueprint to the skeleton mesh, we will click on the existing HeroTPP to display the details panel. Focus on the animation part of the panel; the following screenshot shows the original setting that I have when there is no animation sequence linked to the skeleton mesh and it does not use an Animation Blueprint. Set Animation Mode to Use Animation Asset and Anim to Play to None:

EventGraph

To use MyNPC_Blueprint for this skeleton mesh, set Animation Mode to Use Animation Blueprint. Select MyNPC_Blueprint for Anim Blueprint Generated Class:

EventGraph

Now, compile and run the game; you would see the NPC walking on the same spot with the speed set as 100.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset