Space Shooter Challenge: Aggressive Enemy Type
Time to start adding more enemies! Today’s challenge was to add an aggressive enemy type that tries to ram the player. I found a suitable looking ship that could deal the damage and got to work.
I created a new enemy prefab with the new sprite then assigned basic functionality via it’s ID. Behavior wise I wanted a new Coroutine I would write later.
Then in terms of movement, I wanted the new enemy to move down at all times, I would alter it’s rotation in the Coroutine, but I always want it to continue moving forwards. In terms of the Movement Bounds method I established a bool value to be passed in.
This bool has been setup to discern whether or not the Enemy type reappears at the top of the screen.
Now for the Coroutine. I wanted a continuous updating of rotation while the Player is infront of the Enemy. The enemy always spawns at the top of the screen so I can just run the code straight away while this is true, once it goes past the player, the Coroutine will end but the Movement method will continue.
Inside the while loop I first check if the player is null, this prevents any weird behavior happening if the player dies as the enemy spawns in. Then I used a similar system to my Missile behavior, in which I set a Vector3 direction, then set a Quaternion based on this direction. I also create a step speed for the rotation process before using Spherical Linear Interpolation (Slerp) to change the enemy rotation between it’s current rotation and the target rotation. This loops every frame.
Lastly, I didn’t want this ship to act as a kamikaze pilot, so in the Collision method, if the enemy ID is that of the aggressive enemy, it generates an explosion and then returns.
It may not quite look perfect, but the functionality is in place!
Upon writing this post, I’m thinking of potentially having the player stall, then the enemy can stop, backup, and evade. Would love to hear some opinions!