Before getting things started

Before getting into developing our hit reaction system we just need to make sure that we have everything ready. All we really need for creating such system is a character with a set of animations.

We need at least the following animations:

  • Idle
  • Walk/Run
  • Back hit
  • Front hit
  • Right hit
  • Left hit
  • One attack

If you already have all these animations available in your project wonderful you are ready to go, otherwise no need to be worried.

If you don’t have any animation or character available just go to Mixamo.com where you can create a free account and download as many free characters and animations as you want.

In this tutorial I’m going to use the following assets from Mixamo:

The animations are not the best, but for creating a basic example that will be perfect.

If by any chance you don’t know how to set up the Mixamo character and animation downloaded from the above links, don’t panic. In this video, I go over all the steps you need to follow for catching up.

If you didn’t follow the video before continuing just make sure you create a third person character blueprint new project based on unreal’s template.

Working on the player

Set up the collider

Start by opening your main character blueprint. Go in the viewport and click on add component in the left top panel. From the drop-down list choose capsule collision, rename it to “LKick” and parent it to the Mesh component.

After you are done with that, you’ll have a components panel like the one above. The reason we are choosing a capsule collision over other collider shapes is that the attack animation we are using is a roundhouse kick.

Depending on the type of attack animation you are using you may want to choose a different shape, such as for instance a sphere collision.

Now go ahead and start moving, rotating and scaling the collider so that it’s placed right where the right foot of the character is.

The collider should look more or less like in the above screenshot. Now that the collider is placed correctly, go in the construction script and build the following.

The above node will attach the target (LKick) to the parent (Mesh) at the socket name you specify. In this case, I used a bone called “RightToeBase”. If you don’t know the name of the bone to use navigate to your character skeleton asset and choose a bone from the hierarchy that suits your needs.

Once the node looks exactly as the one above one, click on compile for running the node. Finally, go back to the viewport and re-adjust the capsule collision if it moved.

Set up the attack animation

Lastly, our player needs the ability to perform its attack. Go to your player’s animation attack, right click on it and in “Create” click on “Create AnimMontage”. Open the main player animation blueprint and in the AnimGraph right click and bring up the “DefaultSlot”.

Click on the default slot node. Down in the settings panel click on the search icon next to the slot name so to open the Anim Slot Manager. In here click on Add Slot and create a new slot called “Body”.

After that connect the state machine to the Slot and the Slot to the Final Anim Pose node like in the below image.

Now open the animation montage newly created and set the DefaultGroup.DefaultSlot drop-down list to DefaultGroup.Body.

In the event graph of your main character create two variables, one for the animation blueprint instance and the other one for an animation montage.

Then build the following.

If you can’t properly set up the animation montage read how to set up an animation montage at this link.

Let’s set the collisions

If by any chance you haven’t watched the video linked above, in which I set up the basic scenario for building the system, this may be a good time to create our enemy.

All you need to do is just duplicating your main character blueprint, giving it a new animation blueprint and set up all the animations you have available for the hit reactions.

All you need to do is just duplicating your main character blueprint, giving it a new animation blueprint and set up all the animations you have available for the hit reactions.

If you don’t know how to do that just refer back to this video.

Once you have your enemy ready open the enemy blueprint and click on the mesh component. In the “Details panel”, scroll down to collision and open Collision Presets. Set the Collision Preset to custom and then in the trace responses set the Visibility channel to block.

The Collision settings should look like in the screenshot below.

 

Working on the hit detection

In your main character blueprint in the Components panel select the LKick collider. Go to the “Details panel” scroll all the way down to Events and click on OnComponentBeginOverlap and build the below nodes in the Event Graph.

 

 

 

 

 

What’s happening in the above screenshot is the following:

  • We cast the Other Actor output pin to our enemy blueprint
  • Out of the enemy object coming from the “Cast to” node, we get the capsule component
  • If the capsule component is the same object as the Other Comp output pin, it means we are hitting our enemy’s capsule component
  • Also, we grab couple of variables that we’ll need later, the enemy mesh component and an instance of the enemy

Once we know that we are overlapping with the enemy capsule component, we want to start to trace, however, we’ll need to perform a very short trace and as often as possible in order to be precise.

In this kind of situation, a while loop sounds like the way to go, however, if you’d use the built-in while loop given by unreal you’ll notice that the game will crash because of the number of iteration the while loop would perform each frame.

What we’ll do is using a custom while loop which accepts some delay. Go ahead and create a new macro called WhileLoopWithDelay and make it look like the one in the screenshot below.

 

 

 

 

 

 

Now that our custom while loop is ready, let’s use it. Right after we check if we are overlapping with our enemy capsule collider connect the true pin with what you see below.

 

What about the enemy?

Let’s go over the enemy blueprint now and firstly create some variable as shown here.

Once you created the Right, Left, Front, Back, HitVector and Direction Vector, in the Event Graph right click anywhere, type “Add Custom Event” and create a custom event.

You can name the event whatever you prefer, I will name it “Hit”. The event will accept one parameter called HitVector which will obviously be a vector.

Out of the execution pin from the Hit event just created build the following.

Now you can build what’s in the below screenshot and connect it to the execution pin coming out of the Direction Vector setter node.

What’s going on above there!

What we are doing is the following:

  • We assign the hit vector, which is a direction, to a variable
  • We create a vector which represents the forward direction of the enemy
  • The dot product is used to compare the two vectors just calculated. If the dot product returns -1 the two vectors are looking opposite to each other. If the dot product returns 1 the two vector face the same direction. Lastly, if the value is 0 the two vectors are parallel.
  • In case the value is between zero and 0.7 (this value can be set to whatever suits you best) then the cross product is used to understand if the hit vector is on the left or right of the enemy directional vector.
  • Lastly depending on the value returned from the dot and cross product the boolean variables which trigger the right animation are set to true.

How do the animations play?

Once more if you haven’t followed the link of the video in which I show how to set the basic scenario for the system what happens after the variables are set it’s quite easy. The enemy animation blueprint constantly sets its local variables for playing the animations to the value of the enemy blueprint variables.

Once the animation it’s fired, it blends back to the previous state by checking the remaining time of the animation length. Lastly, the enemy blueprint variables are reset to false using notifies placed at the end of each hit reaction animation.

If you can’t get it to work you can check how I do it in this video.

Final touches and we’re done

We created a nice event up there, however, we should really use it somewhere now.

Go back to your main character blueprint and let’s make a couple of adjustments to the OnComponentBeginOverlap event call.

After we set the variable hit to true at the end of the event OnComponentBeginOverlap add the above nodes.

What they do is firstly playing the animation montage backwards at the exact moment we hit the enemy’s mesh, this will try to prevent the main player foot from going through the enemy’s mesh. Also, it will kind of give the effect of the player’s foot bouncing back because of the hit.

We then call the “Hit” event passing in the Hit Vector from the LKick collider. This vector represents the direction in which the foot is kicking the enemy.

 

The hit reaction system is now finished with those last two nodes set up. I hope you got it all right and you are now enjoying some cool hit reaction animations there. If the system doesn’t work you can follow these videos for extra help:

Hit Reaction Part1

Hit Reaction Part2

In the two videos, I go over every single step you need for setting up the system. If you can’t still set it up correctly just leave a comment below and we’ll try to sort it out together. Enjoy!!!

Comment using your Facebook account