AHMET ALTIOGLU
5 min readJul 4, 2021

How to make a hit animation with code in Unity3D

Hello guys. It’s been a long time since you heard from me. I apologize about it. But, I am back. Today, we are gonna make a hit animation with code in Unity. I am gonna show you everything you need (Scene, Scripts, Video) so that you will be able to implement your version if you wish. Before getting started, I would like to declare a bunch of concepts you must know. If you don’t know any one of them, it’s gonna be hard for you to grasp. So make sure you search and grasp the concepts below.

1-Linear Interpolation (LERP)

2-Coroutines

3- Navigation System

What We Have

Example Scene

We simply have a scene that has a few objects (Player shown in white, Enemy shown in black and a bunch of obstacles shown in red). We want to make a hit animation so that when the enemy is close enough to player, It will move towards the player then go back to its last position. If it does not make sense, let me show you a video example.

A Video Example Of What We Will Achive

Note: We are not gonna deal with player’s movement logic. But, for the simplicity here is the PlayerController Script

PlayerController Script

I want to start by talking about the EnemyController Script which is the one we are gonna implement our hit logic. I am leaving the script down below. Take your time to grasp what we will do in the script, then I am gonna explain each individual concepts EnemyController Script has.

First Part Of the EnemyController Script
Second Part Of the EnemyController Script

FollowTarget Function

In FollowTarget Function, we need 3 private fields;

1- NavMeshAgent to get reference to our NavMeshAgent Component attached to the agent in the editor.

2- A Float variable to use to calculate how close the agent can be to the player.

3- A Transform field to hold reference to the player.

To make the agent follow the player, we need to check whether or not NavMeshAgent Component is active. If it is, we simply create a vector from the agent to the player and pass it to the SetDestination Function with a specified threshold (2 floating point number in this case) so that we prevent the agent from going inside of the player. And, we call this function inside Update.

Note: The reason we normalize the “dirToTarget” Vector is that we need to shorten it to the length of one so that when we calculate the destination for the agent, it will fit in.

CanAttack Function

In CanAttack Function, we simply check whether or not the agent can attack the player. It returns true if it can and false otherwise. To do that at first we get the positions of both the agent and the player. Then, we calculate the square magnitude of distance from the agent to the player. After that we define a variable as agent’s hit range. At the end we have 2 conditions for the agent to be able to attack. And, we call this method each frame.

1- NextAttackTime: If nextAttackTime is bigger than the current time we move to the next condition.

2- Agent Range : If range field is bigger than the distToTarget, it goes in and returns true.

TryToAttack Coroutine

Now, this is the main logic that you need to grasp in order to make The Hit Animation. I am gonna explain each line of the TryToAttack Coroutine so that you will fully understand.

In TryToAttack Coroutine, first we need 3 Vector fields.

1- Player Position

2- Agent Position

3- DirToTarget (from agent to player) Position

We also need to disable agent’s NavMeshAgent Component so that it won’t look silly when the agent attacks the player.

Now, I want you to close your eyes and imagine this in your mind. There’s our agent stopping near our player. And, It is capable of attacking in the current frame. But what do we need in order to make the agent attack the player? The answer is simple. At first the agent will move towards the player then when it is done attacking it will go back to its last position. And, TryToAttack Coroutine will stop. But, How Do We Implement This? Right at this moment, I would like to show you one of the Math’s beauty which is just a graph.

4(-x²+x) Graph

As you see in the graph, it goes from zero to one then back to 0 again. It is exactly what we want right? We need the agent to move to the attack position then move back to its last position. And, we can make this possible by using the equation above.

Now, To implement this we need a floating point number that will increase each frame by deltaTime (defined as percent in this case). Then, we need an interpolation value that we will use in Vector3.Lerp Function. At the end, we just pass the parameters in the Lerp Function and it will do the job for us. But, we don’t forget to enable the agent, otherwise agent will stop right after attacking.

Note: The bigger “attackSpeed” is the bigger “percent” will be so that Hit Animation will be faster.

Conclusion

To conclude, I must say that Math has awesome concepts for us (Game Developers) to use. I just showed you one of them which I think it was the simple yet powerfull Math we don’t talk about. If you have any questions, suggestions, feel free to reach out to me here on Medium or my Linkedin page. And, last but not least I recomend you to watch this video to see a little bit of beauty of Math. Until next time, bye :).

AHMET ALTIOGLU
AHMET ALTIOGLU

Written by AHMET ALTIOGLU

I am a motivated Software Developer.

No responses yet