In this article, I will go over the implementation of the new “Ram” enemy.
The ram enemy keeps an eye out for the player and when they get in range the ram will charge their power cells so they can turn on their shield and afterburners, attempting to ram the player. Once the shield takes damage the ram will continue on its way as the shield gets disabled. It will still try a last-ditched effort to ram the player without a shield.
The first step in the process was to add a new enemy sprite and prefab it. I reused the shield and thrusters from the player. The ram uses the enemy abstract class I created with some overrides.
I have added a sight distance variable that can be changed in the inspector, a float for an invincibility period (for when the ram takes damage while the shield is on), and 2 speed variables (base, and ramming speed).
I have a function that checks the distance to the player and compares it to the sight distance. If the ram “sees” the player it begins a charging routine. The charging routine sets a bool to true which switches the movement from the general enemy movement to a ramming action where the enemy rotates to face the player and begins closing the distance.
The charging routine has a delay after which the shield is activated (if they have not been destroyed previously) and the speed is increased).
If the ram loses sight of the player the charging routine is stopped, speed is reset, shields and thrusters are deactivated.
The death function from the base enemy class has also had an override applied. The enemy will first check for a shield and if there is one it will deactivate it with a flicker effect telling the “Set shield” function that the shield is being destroyed (preventing it from being activated again).
As the shield flickers off the enemy will remain invincible to attack but can still cause damage to the player if they collide again.
If no shield exists and the invincibility period is over the enemy is destroyed like normal.
All that’s left is to test the function.
That’s all for now.