Laserpunk 2076

This is my version of “Laser Defender”:

I suggest playing a number of times because depending on luck the game can be more or less difficult.

For anyone interested here’s a list of features I included that weren’t covered in the course:

  • Bonus spawner
  • Asteroid spawner (asteroids can damage all objects)
  • 3 types of allied ships
  • Force field shield (for the player, 2 enemy types and 1 ally)
  • Projectiles changing layer depending on their velocity (e.g. so that deflected enemy projectiles can damage enemies if travelling “upwards”)
  • Additional enemy wave spawning after “bonus” is picked up (thanks to @Nina for help!)
  • 5 types of firing
  • Health bars (for player, bosses and shield)
  • 3 types of enemies (easy, boss and difficult)
  • Random enemy spawner
  • Increased enemy health and firing frequency depending on the score
  • Animated explosions on point of projectiles contacting colliders

Initially I didn’t plan to add so many features, I just wanted to learn how they work but after they were finished I was so happy that I left them in the “final” version.
I’ll be happy to share the code or explain how everything works, feel free to ask.

Any feedback is welcome!

8 Likes

Hi @tobson,

Thanks a lot for sharing your game. Even though I have played hundreds of Laser Defender versions in the past years, I ended up playing yours for a couple of minutes because it was so enjoyable. The many different special effects make the game look very exciting. There is always something happening that keeps you busy. Great work! I only stopped because work is waiting for me. Maybe I’ll come back at the weekend to play your game a bit longer. :slight_smile:

2 Likes

Nicely done. Lots of new features above and beyond. I’d be interested in seeing your code for how you got the health bars and the power ups.

Thanks @matt_c! Do you remember your score?

I used this tutorial for health bars. I think it should be possible to create just one script/class but I couldn’t do it so instead I created 3 separate classes for player, bosses and shield. The code is simple, what’s not so obvious is how to attach script to gameobject and slider. If you follow the tutorial you should be able to do it. The border is optional, I used it only for the player’s bar.
The code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBar : MonoBehaviour
{
    public Slider healthBar;
    public player playerHealth;

    // Start is called before the first frame update
    void Start()
    {
        // if statement to avoid error when player dies.
        if (!playerHealth) { return; }
        playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<player>();
        healthBar = GetComponent<Slider>();
        healthBar.maxValue = playerHealth.maxHealth;
        healthBar.value = playerHealth.maxHealth;
    }

    public void SetHealth(float hp)
    {
        if (!playerHealth) { return; }
        healthBar.value = hp;
    }
}

The power ups are spawned in similar way as enemies with EnemySpawner. Bonuses have their own layer so you can decide who can pick them up. The exception is laser upgrade which tries to access a variable that only Player has so to avoid errors only the player can pick it up.
The code for bonus spawner:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BonusSpawner : MonoBehaviour
{
    [SerializeField] float timeBetweenBonus = 5f;
    [SerializeField] List<GameObject> bonusTypes;
    //[SerializeField] GameObject bonusType;
    [SerializeField] float bonusFallSpeed = 2f;
    [SerializeField] float spawnLocation = 1f;
    float waitCounter;

    // Start is called before the first frame update
    void Start()
    {
        waitCounter = timeBetweenBonus;
    }

    // Update is called once per frame
    void Update()
    {
        waitCounter -= Time.deltaTime;
        if (waitCounter <= 0f)
        {
            int randomIndex = Random.Range(0, bonusTypes.Count);
            float offset = Random.Range(-spawnLocation, spawnLocation);
            GameObject bonus = Instantiate(bonusTypes[randomIndex], transform.position + new Vector3(offset, 0, 0), Quaternion.identity) as GameObject;
            bonus.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -bonusFallSpeed);
            waitCounter = timeBetweenBonus;
        }
    }
}

Variable spawnLocation is used to randomise the x position of bonus gameobject spawning.
List<GameObject> bonusTypes should be populated with bonus prefabs in Unity Editor. The easiest bonus that I could think of is the health pack which is simply a gameobject with a DamageDealer component with negative damage value. Allies work in the same way as enemies (they use Enemy.cs component and WaveConfig mechanism) but have separate layer for the collision matrix.
Let me know if you are interested in particular bonus type.

Thanks a lot @tobson, this is really helpful. I’ll definitely dive in and see what I can get going from this.

Very nice! My boomer reflexes got me 54420. Things went a bit pear-shaped when my luck ran out and I stopped getting Ally pickups.

Couple of years ago I made a shooter using Godot - gave my phone to a colleague’s kid to playtest… And good lord he got a score I’d never even come close to. Kids :slight_smile:

1 Like

Nice game, the only thing that bothered me is that the ship is too big for my taste, really hard to dodge things.

I love all the pickups and the allied ships were a nice touch. I also thought that it was very realistic in that the allied ships shields block lasers. Very good work!

Privacy & Terms