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 /
avatar image
2
Question by nightowl79a · Jan 06, 2015 at 08:07 AM · collidertrigger

I want 3 gameObjects to take damage every 1 second while staying in a trigger: What I'm getting is each cube taking damage one at a time. Help?

 void OnTriggerStay(Collider other){
         lastDamage += Time.deltaTime;
 
         if (lastDamage >= 2f){
             if (other.collider.gameObject.GetComponent<Actor>() /*&& Time.time >= lastDamage*/){
                 other.collider.gameObject.GetComponent<Actor>().TakeDamage(damage);
                 lastDamage = 0;
             }
         }
 
     }

This is the code I'm using. I have 3 cubes in this trigger with 'Actor' scripts attached, they all have trigger colliders, and kinematic rigidbodys. They are all taking damage, But one at a time. The first one will take damage until it runs out of HP and destroy's it self, then the next cube will take Damage until it destroys itself, ext. What I want is for them all to take damage, then one second later take more damage, they should all run out of hp and destroy themselves at the same time. What I think is happening is the first one is taking damage and the 'lastDamage' float is re-setting making the IF statement fail for the next cube. I have tried putting the timer into the Update statement but that makes it act weird(One cube randomly dies first, followed by the next two at the same time.) I'm not sure what else to do, so if anyone could help I'd be grateful.

Comment
Add comment
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

1 Reply

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

Answer by troien · Jan 06, 2015 at 10:21 AM

This indeed has to do with lastDamage.

First problem: OnTriggerStay can be called MULTIPLE times each frame (once for each collider you hit), it isn't actually smart to increase this timer here, sinse when you are hitting 2 colliders, the damage will be inflicted twice as fast.

Second problem: If OnTriggerStay is called twice in one frame, for 2 different colliders. And lastDamage >= 2. For the first collider it will do damage and reset. The second collider won't take damage because lastDamage is 0 again.

Moving this timer to FixedUpdate should work. (To FixedUpdate because of the execution order) I didn't test this, so there might be stupid errors, but this is what I think should work:

 void FixedUpdate()
 {
     if (lastDamage >= 2)
     {
         lastDamage = 0;
     }
     lastDamage += Time.fixedDeltaTime;
 }
 
 void OnTriggerStay(Collider other)
 {
     Actor actor = other.GetComponent<Actor>();
     if (actor != null && lastDamage >= 2)
     {
         actor.TakeDamage(damage);
     }
 }

Another solution could be to move lastDamage to the Actor script, and handle it like this...

 void OnTriggerStay(Collider other)
 {
     Actor actor = other.GetComponent<Actor>();
     if (actor != null)
     {
         actor.lastDamage += Time.deltaTime;
         if (actor.lastDamage >= 2)
         {
             actor.TakeDamage(damage);
             actor.lastDamage = 0;
         }
     }
 }


On a sidenote, it might be smart to make '2' a variable instead of a magic number, especially if you choose the first of the 2 solutions ;)

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 nightowl79a · Jan 07, 2015 at 01:04 PM 0
Share

This worked perfectly thank you so much! I knew what was happening just not how to fix it. I had also tried a similar solution using Update, and that didn't work properly. Thank you again, there is so much I need to learn still about C#, but this taught me a lot.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

Triggering platform animation on colliding with Button 0 Answers

Use trigger with player but have a collider with everything else? 3 Answers

How do you go from one scene, to another, then a third and back again ? 0 Answers

Stop object colliders (non-mesh) bouncing on collision 1 Answer


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