3D Survival FPS Game

Making the Enemy Damage the Player

Now that the player can damage the enemies, it is time to do the vice versa where the enemies can also inflict damage to the player. Since the enemy isn’t using any Ray Casting ability we will have to be doing the damage method in another way. Since all enemies have an collider we can use an On Trigger Event where we can add another additional collider to the enemy as a trigger function.

Setting Up the Enemy:

Within the enemy prefab, we are going to add a new sphere collider which the “is Trigger” going to be toggled on. We are also going to make the radius of the trigger larger than the enemy so that it can track whenever the player is within the area.

The Spherical Trigger collider that only detects the player.

Scripting the Enemy AI:

There are many ways to script the Enemy attack AI and usually when creating a game, you would pick one method that is most suitable to your needs. In this FPS game we will be focusing on using Enums and the State Machine. State machines, as the name suggest is basically the ability to set definite types of states (just like animation states) a dedicated function.

For this Attack function we are going to create 3 states “Idle, Chase and Attack”.

Then we are going to create a new global variable called “Current state” which we will assign as “Chase” by default.

This way, the enemies when spawn, will have a default state of Chase.

Now these states right now are considered empty, they have no specific functions attached to them. To start, we are going to assign the “Chase” state to our movement script. To do this, we basically create an if statement stating whenever the current state is “Chase” then we shall be allowed to move.

Next we are going to assign the “Attack” state a new function. In this gaming logic we want the enemy to chase up to the player and once the enemy is close enough (aka, when the player has triggered the sphere collider) then we are going to switch the enemy state to “Attack”. When the player runs outside of the sphere collider then the current state would switch back to “Chase”.

To accomplish this we are going to use “On Trigger Events” for both on Enter and also on Exit.

Now that we can freely switch between states, it is time to actually script the Damage component. Since we don’t want the enemy to quickly destroy the Player within seconds it is always best to create a Cool Down system for the attack speed. In order to do that, we need the current time to be compared to the preset next attack duration.

Therefore we will need two global float variables called “Delay Attack” which will be serialized so we can adjust the settings in the Inspector. Then another variable for “Next Attack”, this is the timer that is being compared to the overall elapsed game time.

Next we are going to create a new Attack method which will contain all the functions to attack along with the cool down system.

This basically states that whenever the elapsed game time is greater than our next attack timer, then we are allowed to attack. Next we have to make the “next attack” value equal to the “current elapsed time + the preset delayed attack value”. With this the enemy won’t be able to attack you just as the game begins, and can only attack when once every 1.5 seconds.

Finally within the Update method we are going to make the same declaration for whenever the enemy AI is in the “Attack” state, we will trigger the attack method.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.