Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Slow Damage over time (Fire)

Discussion in 'Scripting' started by apiotuch, Apr 8, 2010.

  1. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    I'm having trouble slowing the damage dealt when my player is in a trigger collider box(standing in fire).

    function OnTriggerStay(collisionInfo : Collider){
    if(collisionInfo.gameObject.tag == "Fire"){
    Health-=FireDamage*Time.deltaTime *1;
    yield WaitForSeconds(1);
    }
    }

    Basically, when I walk in the fire, it waits a second before dealing damage, but as soon as it starts it just deals the damage fast. What I want is a 1 second pause before dealing another hit of damage. Right now, the health disappears incredibly fast. Any help would be great;

    trying to slow health depletion.
     
  2. Mcadieux

    Mcadieux

    Joined:
    Nov 13, 2009
    Posts:
    55
    Your code seems right to me. It will basically deal whatever FireDamage is every second. So if FireDamage is set to ten, it will take ten seconds to deal 100 damage. Did you try just lowering the FireDamage variable? You can also get rid of the *1 at the end, as multiplying times 1 does nothing. You should also get rid of the yield command.
     
  3. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    No, my fire damage variable is set to 1, and starting health is 100. it takes about 1 second for it to deplete completely. I was hoping the yield would pause it between every interval, but it doesn't; it only affects the initial delay before damage begins.
     
  4. Mcadieux

    Mcadieux

    Joined:
    Nov 13, 2009
    Posts:
    55
    I figured out what your problem probably is. I bet your using an int as your health variable. When you do FireDamage * Time.deltaTime you get something like 10 * .02 which gives you a damage of .2 to inflict every frame. If you subtract .2 from an integer it rounds it down so 100 - .2 = 99. This is dramatically speeding up your damage dealt.

    Try changing your health to a float.
     
  5. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    ya that helps, but I was hoping to stick with int values because the design specifies a fixed amount of health; As in 4 or 5 health bars. But I guess I can work around it easier doing it this way.

    Thanks.
     
  6. danielvmacedo

    danielvmacedo

    Joined:
    Nov 2, 2009
    Posts:
    26
    Just curiosity...

    why are you multiplying by * 1? :p

    Daniel
     
  7. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    If your health was an int, how would you display 96 out of 100 as x out of 5 health bars?

    As I see it you have the same problem whether or not your health is a float. Also, you can round the value to an int for display purposes if you so wish.

    Btw, if you have 5 health bars totaling 100 health I see it as a bad game design if you allow the player to stand in the fire for 20 seconds before it visually shows that he's been hurt by the flames. I would rather do it arcade style and have the fire subtract 1 health bar then give the player invulnerability for a few seconds before subtracting another bar.

    Just my $.02 :)
     
  8. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    I'm using 100 right now because It's easier to see how fast the health is dropping, eventually we would use a smaller value, I was using 5 health bars as an example.

    And I figured it out thanks guys. I just lost internet connection for a few hours so I couldn't post until now.
     
  9. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    I use *1 because later I will substitute it with a user defined variable. I just put the *1 in there as a placeholder, sort of a reminder for me later on, when I need to clean up the mess.
     
  10. reigota

    reigota

    Joined:
    Mar 23, 2010
    Posts:
    86
    Hmmm the problem, as I can see, is that OnTriggerStay is called EVERY frame you are inside the trigger. So, the first frame inside will trigger a 1 sec delay, second frame will trigger ANOTHER seconde delay, the third frame will trigger another second delay... got the problem?
    Try something like

    function OnTriggerStay(collisionInfo : Collider){
    if (enable){
    if(collisionInfo.gameObject.tag == "Fire"){
    Health-=FireDamage*Time.deltaTime *1;
    enable = false;
    yield WaitForSeconds(1);
    enable = true;
    }
    }
    }
     
  11. savage_oats

    savage_oats

    Joined:
    May 21, 2011
    Posts:
    1
    Hey Apiotuch, Did you ever fix the problem with the fire damage script?
     
  12. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    reigota

    While something like that should work, it is not worth it. Simply store health as a float and if needed round it for display purposes.