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
0
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
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 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
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 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.

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

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

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

How to Make Health Decrease Over Time 2 Answers

My health bars HealthBar.fillAmount doesnt change.tnx,i nead a help in fillamount :pls my HealthBar doesnt change. i dont know what to do.pls:) 2 Answers

Trying to make a unique type of health 'bar' 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