In this tutorial, we would make the action state Idle of enemy as a pre-action before chasing Player. In concept, when a zombie got shot, it should tracking the source of gun fire by instinct and move toward it. We would create this behavior to our enemy.
In Enemy script, to make a detecting area, we need to set a float to enemy.
With this float, we can detect whether the distance between enemy and Player is less than the detecting range.
Do this checking in Update().
Then we can make the Enemy state change into Chase.
Now our Enemy would chase Player when in range.
Gun shot response
The next would be responding to the gun shot.
As in my project, both Enemy and Player shared the same script to deal with damage. We can simply add some lines in it to make enemy can response to gun shot.
First create a public method in Enemy script to change the state.
Then in Health script, we can call this StartChasing() method when being attacked.
Once done, set the tag of Enemy to “Enemy” to make this code can process.
When you attack an Enemy in game, it would start chasing you!