What a Tricky Enemy!- Game Dev Series 44
Objective: Create a new enemy with unique movement and attack method.
So far we have waves of enemies with 3 different movement and rapid laser attack. These will all become dull to player if we couldn’t make some changes. Create something special(such as a new type of enemy) that out of existed rules might be a good way.
In my thought, an unpredictable enemy would be tricky and full of challenge. I would like to create an enemy with suicide bomb attack when Player is in a range. Even more, it would move as teleport in 5 different spots randomly and leave the scene then destroy if it detects no Player.
Sounds tough? Then that would be great.
Create DetectBomb script
Since it moves unlike any other enemies before, it might not use the same script as them. We should create a new script and I will called it DetectBomb.
Before we start coding, we should figure out what should it move(or attack).
- Moves from top with random x-axis, when reach a specific y-axis spot, start teleporting.
- Randomly choose a spot in a sphere with a customize radius value. Then teleport to that position and stay for 3 seconds and repeat this step to search a new spot.
- If Player is in range of detection, stop teleporting and move towards Player, or if it has already moved 5 spots, go down till out of scene and self destroy.
As a start, create a new method in DetectBomb. Since the movement will repeat the whole part for 5 times, it would be better to run it in a loop with coroutine.
Within that while loop, we will set some statement to it. We can add an integer to counts the times of teleportation.
In that loop, we want it to locate a spot in a sphere, which we will use insideUnitCircle to locate it.
While the bomb has teleported for 5 times, it should go straight down and destroy.
Next we can set it chasing after Player.
However, the chasing movement is in conflict with the teleporting movement. We can use a bool to deal with this.
Create a bool variable and add it into the routing movement.
Then enable it when Player is in chasing range.
Then it should move as you wish. Next we should make it explode when collide with Player or laser. We can simply use the same method from our Enemy script.
Extract good parts from Enemy script
In this script, we will use the codes of laser collided detect and Player collided detect. We can type these codes into our DetectBomb script.
While we do not have the specific explosion animation with bomb, we can simply use the explosion prefab which we used for asteroid and Player explosion.
Once we done with collider, we can put it into SpawnManager.
Start spawning it
Before we spawn it in routine, we should set the frequency. It should not show up with a high frequency or it would be too hard to our players. Spawn once after 4 times of spawning normal enemies might be reasonable.
Tidy up some detail
As in DetectBomb script, we use 2 float to calculate the radius, which is hard to adjust the actual range. We can use OnDrawGizmo to visualize it in preview window.
With these codes and SerializeField the float variables, it should be easy to adjust them in editor.
That’s all the setting for this unique enemy. We can try it now!
Next: Reasonable Spawning Rule