Creating a Laser Sword Weapon
Adding to the features of our Space Shooter game, I think it’s time for a cool new weapon! While I was thinking of doing a projectile, I think I will save that for later.
But now that our Player’s Shields can withstand some more impact, what goes great with a shield?
A sword! A LASER SWORD to be exact!
In this article we’ll create a new weapon that will slice through any Enemy, so long as you’re close enough and willing to take a hit if you’re not careful!
The Powerup
First, we can create the Powerup collectible that will activate our Laser Sword.
I found this Sprite in a free pack on OpenGameArt.org and simply animated it to change colours like I did with the Plus 1 Health pickup.
I attached the usual RigidBody2D and Box Collider2D, and also added the Powerup script:
Then added our sword to the switch statement:
The Laser Sword
Now it’s time to actually make the Laser Sword. Or, 3 lasers that look like a sword.
My Laser Sword consists of 3 Laser Prefabs, scaled and positioned to meet at a point:
These Lasers have their speed set to 0 so that they don’t fly away.
I’ve also created a bool variable in the Laser script (_isLaserSword) to denote if they are a part of the Laser Sword or not. This will stop each Laser from getting destroyed when the Sword comes in contact with an Enemy.
Like the Player Shield, the Laser Sword will be a child to the Player, and enabled when the Powerup is collected. Then disabled with the help of a Coroutine.
In the Player script, we’ll create a [SerializeField] reference to our Laser Sword (_laserSwordVFX) and link it in the Player’s Inspector.
Now we can properly activate the sword:
And you will have noticed the for loop during activation. This is to make sure that each laser in the Laser Sword is correctly being identified as a Laser Sword part:
Fixing the Enemy script
Now we just have to change some code within the Enemy script for the sword to continuously slice through Enemies and not get destroyed in the process.
In the Enemy OnTriggerEnter2D, when colliding with a Laser, as long as the Laser is NOT a Laser Sword, then it can be destroyed:
And now we have a super awesome Laser Sword that we can use to slice and dice as many Enemies as we can (in the span of 5 seconds)!
In the next article we’ll be creating a Camera Shake effect when the Player gets damaged.