2D Shooter: Adding an Aggressive Enemy
Objective: Create an enemy type that aims to ram itself into the player. It also takes multiple hits to be defeated.
So for this enemy, I want it to essentially be tank-like. All it aims to do is try to kamikaze itself onto the player. It takes three hits to take down. To balance it out, it doesn’t shoot and it moves slower than the basic enemy.
Here’s what I made this tank enemy look like:
Its basically just the same as the enemy except its a different color and its bigger. About 1.5 times bigger. I also attached the damaged engine to a couple places so that it looks like it breaking apart as it gets hit. I did this by taking one of the engines from the player, duplicating them onto as children objects on the enemy, and flipped them.
Now I thought it would be easier making a separate script for this enemy, so that’s what I did. A lot of it will be quite similar to the enemy script, but one of the biggest changes will be how it moves.
First we want to make sure we have access to the player script.
Then we want to store the player’s transform component in a variable.
Now it’s time for the movement. To get it to move towards the player, we’ll want to use Vector3.MoveTowards(current position, target position, speed). We only want this, if the enemy is above the player. If it ends up below the player, we want it to head straight down, and then respawn at the top if it goes off screen.
We also want it to give it a way to keep track of life.
Now we make a method using OnTriggerEnter2D, where it will detect collisions from the player and the laser, and react accordingly.
In the same method, we’ll make a couple if statements for when the life value goes down.
This is what will enact the damage animations (I renamed the damaged engines to hits) to activate, and give what the spawn manager needs to react to its death.
Now we having a tank enemy! Next time we’ll work on an enemy that detects and shoots powerups, and aims its laser at the player.