Day 43 | More Enemies (Aggressive): 2D Space Shooter Series

Ted Lim
2 min readJun 10, 2021

In the next upcoming articles, we’ll focus on adding diversity to our pool of enemies to create more exciting gameplay.

Aggressive Enemy

The aggressive enemy that we will make will detect when the player is within a certain range. Once the player is within the range, the enemy will speed up in an attempt to ram into the player’s ship.

To do this, we need the enemy to know when the player is within a certain distance in front of it. Once in front, we need the enemy to increase its speed.

Detecting the Player

To get the reference to our player, we can do 1 of 2 options. We can use GameObject.Find() to find the Player GameObject in our hierarchy, or we can add a trigger collider to our enemy that will look for when the player enters the trigger. The Space shooter game is relatively small, so we’ll use the Find method for now.

Determining Distance

To get the distance from the player, we can use the Vector3.Distance() method. If the enemy is within a defined range, we double its speed. Then, if the enemy is outside of the range, we return its speed to normal. In the Update() method, we have to add the calls to our functions that update the enemy’s speeds.

Then, in our functions, we check if the distance between the enemy and the player is less than the _aggroDistance, which has a SerializeField attribute.

Since the new enemy inherits from our previously created EnemyBaseScript, we already have all the other necessary behaviors for our enemy, and our new enemy is completed!

--

--