About 'Create Damage Dealer Class'!

In this video (objectives)…

  1. Create a class called Damage Dealer that can be placed on whatever game objects we want to use to inflict damage.
  2. Create an enemy class that can handle enemy's health.
  3. Shoot a laser that harms enemy.

After watching (learning outcomes)… Create a class responsible for dealing damage to objects which have health.

(Unique Video Reference: 19_LD_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Hello Rick

Why do you take a float variable:
Snag_18a090

I don’t understand this? The damage variable is also an int.

I thank you very much for this excellent course. I enjoy your various courses (2D, 3D, RPG …) since several weeks and I learn so much!.

Norbert

Hey Norbert,

You could totally just have health as an int. Or you could have damage as a float. I don’t think there’s going to be a huge difference either way.

Mostly, my reasoning is that for damage I am pretty certain that I’m only ever going to want to use an integer and not get finickity with how much damage is dealt - no decimals required. But with health, there may be some modifying variables that could require additional calculations, such as adding a shield or multiplier to damage or something else that might result in wanting health calculated to a finer degree.

I hope this makes sense. The bottom line is, no great reason, just that it doesn’t really make much of an impact.

2 Likes

I typed up the EnemyDealer 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 the Enemy script here:

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

public class Enemy : MonoBehaviour
{
    [SerializeField] float health = 100;

    // Use this for initialization
    void Start()
    {

    }

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

    }

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

I set the health and the collider like the video


I set it up like this:

I also attached the damageDealer to the laser like so:

But when I play the program, and I shoot at the collider, the health doesn’t return:

Why not?

hi Ahron! I had the same issue but fixed it by retyping the entire ‘OnTriggerEnter2D’ part (in Enemy.cs). Could be I had a typo, but I think not since I’ve checked to the letter several times. I suspect there might be a bug since this isn’t the first time where I retype, or even cut and
re-paste, a few lines after which my code DID work. (I’m using unity 2018.3.11f1 on a Mac)

Anyway, hope this is helpful even though you posted this half a year ago :stuck_out_tongue:

Hello Rick, im just curious, why we dont use If( ) method to bring damage to enemy?

Privacy & Terms