Day 44 | More Enemies (Smart): 2D Space Shooter Series

Ted Lim
2 min readJun 11, 2021

Continuing to diversify our enemy behavior, in this article, we’ll build an enemy that can shoot lasers behind itself.

A Smarter Enemy

A smart enemy doesn’t shoot lasers haphazardly. A smart enemy will shoot lasers backward only when the player enters its line of sight. This means that the enemy needs a reference to the player to know the player’s position.

Like the Aggressive enemy that we built previously, the enemy inherits its reference to the player from the base enemy class. Therefore, we only need to control when the enemy fires backward toward the player. We’ll create a fireRange value to create a firing zone to determine when the enemy will shoot the player.

This method needs to be continuously called to determine if the player’s new position is in the firing zone. We will call it in the Update method. This is why we include a boolean canFireBehind to ensure the enemy only fires once each time it enters the zone. Once the player exits the zone, then the enemy can fire again.

--

--