Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Neuroticaine · Dec 12, 2011 at 12:58 AM · damagetimers

Damage Over Time problem

I'm in the process of making a sidescrolling shooter sort of game in a fantasy setting. One of my player's abilities is to conjure a flame wall anywhere on the terrain. When an enemy walks across flame wall particle system's prefab's box collider, it sets the bool isOnFire == true in that enemy's script. The enemy script also inherits flameWallDuration, flameWallTimer, flameWallDelay and flameWallDamage from another script. All floats. flameWallDuration is set to 2, both flameWallTimer and flameWallDelay are set to 0.5f, and flameWallDamage is set to 20. This particular enemy's health, monsterOneHealth, is equal to 30. The following is the relevant script:

 using UnityEngine;
 
 using System.Collections;
 
 public class MonsterTwoScript : AttackScript
 {
     public GameObject monsterTwo;
     private float monsterTwoHealth = 30;    
     
     private bool isOnFire = false;
     
     void Start () 
     {
         this.flameWallDuration = 2;
         this.flameWallTimer = 0.5f;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(monsterTwoHealth <= 0)
         {
             Destroy(gameObject);
         }
 
         Destroy(gameObject, 8);
         
         if(isOnFire == true)
         {            
             this.flameWallDuration -= Time.deltaTime;
             if(this.flameWallDuration != 0)
             {
                 this.flameWallTimer -= Time.deltaTime;
                 if(this.flameWallTimer < 0)
                 this.flameWallTimer = 0;
                 if(this.flameWallTimer == 0)
                 {
                     this.monsterTwoHealth -= (flameWallDamage/4);
                     this.flameWallTimer += flameWallDelay;
                 }
             }
 
             if(this.flameWallDuration < 0)
                 this.flameWallDuration = 0;
             if(this.flameWallDuration == 0)
                 this.isOnFire = false;
             
             if(this.transform.childCount > 0 && isOnFire != false)
             {
                 GameObject fireEffect = this.transform.GetChild(0).gameObject;
                 fireEffect.particleEmitter.emit = true;
             }
         }            
     }
     
     void OnTriggerEnter (Collider other)
     {    
         if(other.gameObject.tag == "FlameWall")
         {
             isOnFire = true;
         }
     }
 }

The problem is, whenever this enemy catches on fire, it will either not take any damage at all or it will continue to take damage well past the end of flame wall's 2 second duration, and it will eventually kill the enemy, which shouldn't happen since it should only take flameWallDamage/4 (5 damage) a total of 4 times (20 damage), and the enemy has 30 health.

I'm new to programming, and I've learned most of what I know by trial and error, but I can't seem to figure this out. Throwing a bunch of "this" tags onto it was my latest effort to get things to work right, but I still cannot figure this out.

Could someone please help me out here?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Dec 12, 2011 at 01:21 AM 0
Share

Try making most of the vars public, so you can see them move. Or else toss in a big Debug.Log("duration="+ ... ); and you'll be able to search through the numbers later.

I'm also not sure about Destroy(...,8); That should kill you after 8 seconds, no matter what.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by syclamoth · Dec 12, 2011 at 01:23 AM

What you are doing here is a pretty inefficient way of doing things. Thankfully, coroutines are here to save you! Try putting that functionality into a coroutine which gets set off when the monster goes into the fire.

 // We use this to make sure the monster doesn't get set on fire twice!
 private bool onFire;
 // Drag and drop the particle emitter in the inspector. It's much faster.
 public ParticleEmitter fireEffect;

 IEnumerator DoFireDamage(float damageDuration, int damageCount, float damageAmount)
 {
     onFire = true;
     fireEffect.emit = true;
     int currentCount = 0;
     while(currentCount < damageCount)
     {
         monsterTwoHealth -= damageAmount;
         yield return new WaitForSeconds(damageDuration);
         currentCount++;
     }
     fireEffect.emit = false;
     onFire = false;
 }

 void Update()
 {
     if(monsterTwoHealth <= 0)
     {
         Destroy(gameObject);
     }
 }

 void OnTriggerEnter(Collider other)
 {
     if(other.gameObject.tag == "FlameWall" && !onFire)
     {
         StartCoroutine(DoFireDamage(flameWallTimer, 4, flameWallDamage);
     }
 }


A few other things- you say that you have inherited a lot of 'stats' variables from a parent class. It's object-oriented best practice to avoid doing this when you can- it's better to have a 'stats' class which you attach to ever other class that needs them- this way you don't have to assume (or keep track of) any inherited behaviours from the parent class. So-

 [System.Serializable]
 public class Stats{
     public float HP;
     public float damage;
     public float flameWallDuration;
     public float flameWallDamage;
     // and so on...
 }

Then, in every class that uses it-

 // This will show up in the inspector under a little drop-down arrow
 public Stats myStats;

Every time you want one of your monster's stats-

 myStats.HP;
 myStats.flameWallDamage;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Neuroticaine · Dec 12, 2011 at 02:19 AM 0
Share

This works perfectly, mathwise! Thanks a ton for showing me an alternative to my ridiculously clunky timers. There is one problem, though. The particle effects aren't emitting. I've unparented the fireEffect particle system from the monster, updated the monster's prefab and dragged the particle system into the spot for it in the editor, and I've even tried reparenting the particle system back onto the moster as well, and the particle system just doesn't want to emit. $$anonymous$$gestions?

avatar image syclamoth · Dec 12, 2011 at 03:09 AM 0
Share

Well, I can't really explain that. The fireEffect should definitely still be a transform child of the monster, though. They should be part of the same prefab.

avatar image Neuroticaine · Dec 13, 2011 at 05:12 PM 0
Share

Just wanted to update you on the situation. Apparently, I'm just dumb. I didn't reset the position of the particle system to 0, 0, 0 when I attached them to the game object. It was working perfectly the whole time, the particle system was just way off the visible screen. Thanks again!

avatar image Plajen · Jun 26, 2019 at 07:57 PM 0
Share

@syclamoth Hey there! I know it's been many years, but if you're out there and willing to help, I'd be ultra grateful!

I'm new to Unity and C# and my question is: when you do "public Stats mystats;" and then alter the "myStats.HP" variable, doesn't it change the "HP" variable on the "Stats" class itself? $$anonymous$$eaning that if many GameObjects use it, all of them will take damage at once?

avatar image
1

Answer by Neuroticaine · Dec 12, 2011 at 02:51 AM

Thanks for the response. I'll give it a shot. I know a lot of it's extremely inefficient, but this is a continuation of a project I did at the start of the semester and I simply didn't have the time to start the scripting from scratch to make it more efficient. In retrospect, I would have made a completely separate class like you suggested. In fact, I've been working on this so much lately that when I'm in bed, trying to fall asleep, I start mentally mapping out how to make it more efficient the next time around. =P

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clamchoda · Feb 02, 2017 at 09:55 PM 1
Share

This is where great functionality is born.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Health bar help 1 Answer

Survival Shooter cant hit the zooooooom bunny 0 Answers

How do I make the object apply damage on trigger? 0 Answers

Damage two objects based on their relativ speed to each other 2 Answers

The damage to my player keeps stacking even after I quit the game and restart. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges