Search Unity

C# - Death on monster in clicker 2D.

Discussion in 'Scripting' started by Michal_Korek, Mar 29, 2017.

  1. Michal_Korek

    Michal_Korek

    Joined:
    Mar 17, 2017
    Posts:
    16
    Hi guys!

    I'm looking for a tip how to make a death script.

    I mean when I tap the screen(That will be clickergame) and mob have 0 hp then he die and resp next.

    My problem is in code.
    Take a look:

    Code (CSharp):
    1. if (current_hp_mob < 0) {
    2.  
    3.             current_hp_mob = constant_hp_mob;
    4.             Click.gold += gold_per_mob;
    5.             int i = Random.Range (0, enemies.Length);
    6.             enemies [i].SetActive (true);
    7.             a = i;
    8.  
    9.  
    10.         }
    11.         else
    12.         {
    13.             if ( current_hp_mob < 1 ) {
    14.  
    15.                 enemies [a].SetActive (false);
    16.  
    17.             }
    So when I have 'tap_dmg' = 1 then all is right. But when I have tap_dmg = 2 is'nt.

    I know because. Because when hp is = 1 so simple math, 1 - 2 = -1, and mob are 'hiding after death' when is <1 but when <1 and <0 no.

    Have you any tips? I dont want done code, just a simple tip. :)
     
  2. joshmond

    joshmond

    Joined:
    May 28, 2012
    Posts:
    34
    One way you could solve this issue is by doing a calculation for the health and checking against that. What I would do is create a damage method which has a parameter and do something like this:
    Code (CSharp):
    1.  
    2. public void Damage(float amount)
    3. {
    4.     if((currentHealth - amount) < minHealth && isDead)
    5.     {
    6.          currentHealth = minHealth;
    7.           //Spawn loot
    8.          // Instantiate monster
    9.      
    10.     }
    11. }
    12.  
    Something like this should prevent your health from going to minus figures. If you have any more trouble let me know.


    Regards

    -Joshmond
     
  3. Michal_Korek

    Michal_Korek

    Joined:
    Mar 17, 2017
    Posts:
    16
    Thanks joshmond! Thats great tip! :)