Creating a Enemy that Rams into Player when in range

Day 44- Making 2D Space Shooter Game

Objective: Making enemy ram into player when within enemy attack range

First we need to spawn the enemy that is going to ram into player

Then create a new enemy prefab and drag it in the prefab variable.

Now create all the necessary variables in a new script for the new enemy so that it is easy to understand rather than creating cluster of scripts in one single enemy script.

Now create a reference to all the necessary components. We are referencing a player transform to access player position and player script to access public functions.

We can then check if the player is alive or not.

Now we are going to move enemy if not dead and only fire laser when no moving towards enemy.

For the movement we are going to make player move down by default and if the player enters within certain distance from the enemy, the enemy will start following player until it rams into it or the player moves out of its range.

We also not only want our enemy to moves toward player but also rotate so it is looking towards our player when ramming into it.

You can then set trigger to deal damage to the player when rammed into and destroy the enemy when hit by laser and player.

Unity Developer