Creating a Laser Sword Weapon

Frank Warman
3 min readMay 19, 2021

--

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!

Cue sword slashing SFX!

The Powerup

First, we can create the Powerup collectible that will activate our Laser Sword.

My laser Sword Powerup

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:

Powerup ID 5

Then added our sword to the switch statement:

Will be adding functionality to ActivateLaserSword() in the next section.

The Laser Sword

Now it’s time to actually make the Laser Sword. Or, 3 lasers that look like a sword.

Laser Sword!

My Laser Sword consists of 3 Laser Prefabs, scaled and positioned to meet at a point:

Detached from the ship

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.

Laser Sword Inspector

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:

Activating the Sword (Make sure the sword is disabled at start-up)
Laser Sword cooldown

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:

To double check, incase you missed it in the Inspector.

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:

Normal Lasers get destroyed, Laser Sword lasers do not!

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)!

schwing!

In the next article we’ll be creating a Camera Shake effect when the Player gets damaged.

--

--

Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles