• 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
Question by CeratoRaptor · Nov 07, 2021 at 02:30 AM · healthbarhealth barhealth-deductionhealth

Player health dropping far to fast

So, I'm making a 2D game, and in the game the player takes damage when the enemy collides with the player, however, when the player collides with the enemy, the players health drops to zero almost instantly. I was wondering how I could fix this. I have tried looking for a solution without asking, and I couldn't find anything.

My code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class playerHealth : MonoBehaviour {

 public int Health = 100;
 public GameObject player;
 bool damage;

 void OnCollisionEnter2D(Collision2D Col)
 {
     if (Col.gameObject.tag == "Enemy")
     {
         damage = true;
     }
 }

 void OnCollisionExit2D(Collision2D Col)
 {
     if (Col.gameObject.tag == "Enemy")
     {
         damage = false;
     }
 }

 void Update()
 {

     if (Health <= 0)
     {
         Debug.Log("Died");
         //Destroy(player);
         //Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name);
     }

     if (damage)
     {
         StartCoroutine(takeDamage());
     }
 }

 IEnumerator takeDamage()
 {
     while (damage)
     {
         Health -= 5;
         yield return new WaitForSeconds(5);
     }
 }

}

If someone could help, that'd be really appreciated. Thanks.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Natrx · Nov 07, 2021 at 02:45 AM

try changing the collision detection on the rigidbody of the player. I'm not sure which option for the collision detection will work, but just test them all, hopefully that fixes your issue!

Edit: if that doesn't work, you can also try to add a cool down for attacking, or try adding the WaitForSeconds before you are taking damage. And if all that doesn't work, try using a different way of taking damage, without using a while loop. (I'm a beginner at coding so Im not sure what will work)

Comment

People who like this

0 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 CeratoRaptor · Nov 07, 2021 at 05:26 PM 0
Share

Thanks, the last thing worked for me, I created a float timer so that I could track how long it has been since the player took damage, and then every time the timer hit 0, I would subtract damage. You're a lifesaver. Thank you!

avatar image Natrx CeratoRaptor · Nov 07, 2021 at 05:58 PM 0
Share

Great! Glad I could help.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

132 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 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

How to make my health decrease? 1 Answer

Health Code, Damage and Healing 0 Answers

unity health bar not losing health 1 Answer

I need some help with a Fractional Heart System? 1 Answer

How to Make Health Decrease Over Time 2 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