Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jul 13, 2018 at 10:49 PM by Sazails for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Sazails · Oct 18, 2017 at 04:15 PM · collisiondamageienumerator

On collision deal damage each few seconds and doesn't keep consistent collision?

Hi, I am experiencing bit of an error. I am trying to make so that every time that the enemy is colliding with the player, it will deal damage to player every few seconds.

These both are attached to the player-health script:

alt text

alt text

I am ok with coding so I don't need a script, however I would need a good explanation for it. Script would be nice to see and obtain information from too.

Thanks

untitled.png (22.0 kB)
untitled.png (2.0 kB)
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

  • Sort: 
avatar image
2
Best Answer

Answer by JoeriVDE · Oct 18, 2017 at 06:28 PM

I would use OnCollisionStay and then just add Time.Deltatime to the timer so it increases each frame the collision is hitting.

Example script (didn't test this though). Note: this won't work with multiple enemies.

 public class CollisionDamageTimer : MonoBehaviour {
 
     // Timer to track collision time
     float _timeColliding;
     // Time before damage is taken, 1 second default
     public float timeThreshold = 1f;
 
     // player health I assume?
     int _currentHealth = 100;
 
     // called when first colliding
     void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.tag == "Enemy") {
             // Reset timer
             _timeColliding = 0f;
 
             Debug.Log("Enemy started colliding with player.");
 
             // Take damage on impact?
             PlayerDamage(5);
         }
     }
 
     // called each frame the collider is colliding
     void OnCollisionStay(Collision collision) {
         if(collision.gameObject.tag == "Enemy") {
             // If the time is below the threshold, add the delta time
             if (_timeColliding < timeThreshold) {
                 _timeColliding += Time.deltaTime;
             } else {
                 // Time is over theshold, player takes damage
                 PlayerDamage(5);
                 // Reset timer
                 _timeColliding = 0f;
             }
         }        
     }
 
     void PlayerDamage(int amount) {
         _currentHealth -= amount;
         Debug.Log(_currentHealth);
         if(_currentHealth <= 0f) {
             Die();
         }
     }
 }


Comment
Add comment · Show 2 · 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 Sazails · Oct 18, 2017 at 07:13 PM 0
Share

Wow, thanks it actually worked. 4 hours on finding a script when it looks like this. Thanks :)

avatar image JoeriVDE Sazails · Oct 18, 2017 at 09:07 PM 0
Share

Glad I could be of help! ;)

Follow this Question

Answers Answers and Comments

116 People are following this question.

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

Related Questions

Rocket projectile damage not applied on collision (message not sent/received) 1 Answer

Damage by collision with ridgidbodies 1 Answer

Copy a damage value on collision 2 Answers

How to Detect Collision, Calculate Velocity+Mass, Apply Damage 0 Answers

Damage on prefab collision? 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