Objective: To finish basic setup of enemy
In the previous article, I set up the enemy movement and damage. In this article, we will add a couple more features to finish.
We would like to add a blood splatter effect to the enemy when they are shot.
We can update the Shoot script when the enemy takes damage, to instantiate a prefab of the blood splatter.
Modify Splatter Behavior
As you see in the example, the blood splatter effect is hitting the sphere collider. We need to refine the code to essentially ignore the sphere collider so the blood splatter can instantiate on the surface of the enemy.
The first step is to separate the sphere collider (used as an attack radius for the enemy) and attach to a child empty game object on the enemy, and give it its own layer name.
We will then update the EnemyAI script, and remove the OnTriggerEnter and Exit functions and attach them to a new script on the AttackRadius object.
In the EnemyAI script, we can make two new public methods to set the EnemyState. When the AttackRadius object detects OnTriggerEnter, that means StartAttack. When the AttackRadius object detects OnTriggerExit, that means StopAttack.
The last modification is to use a LayerMask on Physics.Raycast to assign specific layers that can be hit, which means other layers are ignored. In our case, we have the AttackRadius on its own layer, and we can assign the Enemy its own layer.
Let’s see if this works.
Blood Splatter Clean Up
As you see, the blood splatter objects are not destroyed in game. Also, the blood splatter effect is instantiating inside the enemy.
We can modify the Character Controller Radius on the enemy to make the blood splatter look more like it occurs on the surface.
Then, we can create a script on each instantiated blood splatter and have it destroy itself just after the animation completes.
Here are the final results.
Thank you for your time!