Unity 2D – Laser Defender (4)

Continued

Damage dealer

Create a new script file in our script folder. Add code below into the new script:

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

public class DamageDealer : MonoBehaviour
{

    [SerializeField] int damage = 100;

    public int GetDamage()
    {
        return damage;
    }

    public void Hit()
    {
        Destroy(gameObject);
    }
}

And then add this script to Laser prefab. Here I faced one problem that I can’t add my script into it, to fix that, I create one new script from prefab rather than the folder, then it is fixed.

And next I need one mechanism to let something has health and could be destroyed. To do this, we need to create a new script named Enemy:

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

public class Enemy : MonoBehaviour
{

    [SerializeField] float health = 100;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        health -= damageDealer.GetDamage();
    }
}

Now add our new script into the enemy prefab, and we can set circle collider 2d on one prefab for testing if the health decreasing normally.

Destroy

And we could use this simple code to make enemy destroyed:

        if (health <= 0)
        {
            Destroy(gameObject);
        }

Make enemy shoot

Add fire() code from player.cs to enemy.cs. And drag new laser for enemy using.

Health System

Copy codes about hit from enemy.cs, add health variable on player, and add collider on player.

Layer Collision Matrix

Now I am going to use layer collision matrix to fix some bugs. Click the player(or others) in the hierarchy, and expand the layer, add "player", "enemy" … into layers. And then set players’, enemy’s, … layer as the related one in prefab, and now open the edit -> Project Setting again, to set the correct collision matrix.

Destroy the missile

Add code below to player and enemy:

damageDealer.Hit()

Scrolling the background

First create a 3DObject -> Quad in our hierarchy, which is like a surface of one cube. And we should adjust the size of the Quad we just created to cover the whole background. Change the texture of background to default, and change the wrap mode to repeat, finally reset the prefab folder.
And then create a new script file for the background scrolling.

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

public class BackgroundScroller : MonoBehaviour
{
    [SerializeField] float backgroundScrollSpeed = 0.5f;
    Material myMaterial;
    Vector2 offset;

    // Start is called before the first frame update
    void Start()
    {
        myMaterial = GetComponent<Renderer>().material;
        offset = new Vector2(0f, backgroundScrollSpeed);
    }

    // Update is called once per frame
    void Update()
    {
        myMaterial.mainTextureOffset += offset * Time.deltaTime;
    }
}

Particle

Create a effect -> particle in the hierarchy. Then I can adjust the attributes of the particle by self.

Explosion

Drag the stars explosion assets to a new folder, and create a material for it. Pick the start explosion from Albedo. Change the shader to alpha blended. Then create a new particle system in the hierarchy. Come to the render and change the default material. Here is a lot of setting, and set whatever you like. Finally, add the code below to enemy.cs and we are done.

    private void Die()
    {
        Destroy(gameObject);
        GameObject explosion = Instantiate(deathVE, transform.position, transform.rotation);
        Destroy(explosion, timeExp);
    }

Leave a Reply