Gun Shot Detected!- Game Dev Series 132
Quick guide: make enemy change their action state into Chase when got shot.
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.
Detecting range
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!