Unity 2D space shooter update – adding a laser weapon

I recently had a nice, month long holiday in South Africa. Apart from affording me some well deserved downtime and a “castaway-like” beard, this meant I was also able to get quite a lot done on my re-working of “Cosmosis” (my original iPhone/iOS app) using the Unity engine. This time the game will be developed for just about every platform out there – one of the best features of the Unity engine! Being a re-write from the ground up, this has meant I have also been adding a lot of new features and new systems to my Unity 2D space shooter (shmup) project.

This post covers the work I did on a new weapon – the “arc laser”. Here is a quick demo of a short burst fire on the laser:

arc laser demo


Currently, once the laser is picked up, you get around 10 seconds worth of firing out of it. After this it runs out and you switch back to your primary cannon fire (or your primary cannon fire plus rockets if you had previously collected that weapon). The laser also features a small animation on the player’s ship, which is the laser arc cannon mount point, which pops out as you start firing. This needs a little bit of tweaking as the position is slightly out at the moment.

Here is how I achieved the laser effect for anyone interested:

  • Two line renderers, one with a small transparent PNG texture with a gradient going from light to darker blue. Each line renderer is attached to a gameobject on the player’s ship
  • The primary beam line renderer has two points, and a 2D collider. When the beam fires, in code I set each point of the line renderer according to the player’s position, and a point off screen. I also activate the collider which is disabled by default.
  • The secondary line renderer is what I use to achieve the arcing effect. This has around 20 points, which I manipulate using the Mathf.Sin function. (See further below).
  • When firing, a float value is tracked, and the deltatime from the game’s update loop is subtracted from this to determine how much more “ammo” your laser has. Once it runs out, the line renderers are disabled and the weapon removed.
  • Taking your finger off the trigger so to speak also results in the line renderers and the collider being disabled (along with the sound effect for the laser).
  • Damage is applied to enemies when the laser’s collider hits them. Of course, damage needs to be incremental, rather than once off like other projectile weapons are, so a timer is used to damage enemies based on how much time they spend colliding with the laser beam.

Here is the basic code I use to manipulate the points on the arcing laser beam, giving them the animated effect you see above:

for (int i = 1; i < laserSegmentLength; i++)
	var pos = new Vector3(i * 1.2f, Mathf.Sin (i + Time.time * Random.Range (0.5f, 1.3f)), 0f);
	laserLineRendererArc.SetPosition(i, pos);


Well, that covers the laser weapon update. There are plenty more changes that have been made recently, so I’ll have to cover those off in another post!

Leave a Comment