Progress Review: Rounding out Player Abilities
I’m noticing that the new feature I’ve added to my 2D shooter game in Unity is making life a little too hard for the player. It’s time to give the player another trick to compensate.
In this specific case, the enemies have gained the ability to destroy powerups they see in front or behind them with their laser turrets. This is especially impactful with the Sinusoidal Weaver enemy type, as it covers a wide swath of the horizontal play-field with its movement. If there are three or four Weavers on the field they’ll almost certainly keep the player from regaining ammo. I like this added pressure to deal with the hard-to-hit weaver early, but running out of ammo too often is already a risk I’ll need to patch later. Lucky for us, the next player feature will help immensely. It’s time to implement the Tractor feature.
Since this is player input based, I’ll start in the Player’s Update method. When I press the C key, I’ll want to activate the visual element of the Tractor. Then I need to access the public list of powerups and iterate a loop on it, checking the range to each powerup. If the powerups are within range (a 5-unit radius) then we trigger a method in the Powerup class that alters the vector of the object toward the player.
Here’s what that looks like in code:
I discover the range with Vector2.Distance, and the bearing vector through simple subtraction (from the perspective of the powerup, so I don’t have to convert it). Then I pass those along to the public Powerup.TractorBeam() method:
And in the Update method of the Powerup class:
All together it works and looks great.
The next thing I want to add is a new weapon the player can use to clear the screen. This will be very helpful in later levels. I’ll call this ability the EMP burst.
This one is simple enough. Create an object that grows to encompass a given area of the screen, give it a collider and bit of code that destroys enemies it encounters.
On update, the field grows until it reaches max size. When it’s boundary is broken, it triggers an explosion and instant enemy death.
That’s all for new player powers. The next section will be all about the final boss encounter. And let me tell you, it is one big, bad boss.