• 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 ilgazalsancak2006 · May 28, 2020 at 09:09 AM · 2d collision

Health Bar Doesn't Go Down When Colliding With Enemy

I was following a few tutorials and now I have a health bar on top of the screen. The bar goes down when I jump (I wanted to test if it works). And the player dies when the health bar is empty. How can I make it so the health bar goes down when it touches an enemy?

Here is my script:

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

public class Health : MonoBehaviour

{ public int maxHealth = 100; public int currentHealth;

 public HealthBar healthBar;
 // Start is called before the first frame update
 void Start()
 {
     currentHealth = maxHealth;
     healthBar.SetMaxHealth(maxHealth);
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         TakeDamage(10);
     }
     if (currentHealth <= 0)
         Destroy(gameObject);

     

}

 void TakeDamage(int damage)
 {
     currentHealth -= damage;

     healthBar.SetHealth(currentHealth);
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SteenPetersen · May 28, 2020 at 09:20 AM

You can use FillAmount in your SetHealth method. So that the fill of the image that represents the health is directly connected to the 'currentHealth' variable. You can follow t$$anonymous$$s tutorial if it is not already t$$anonymous$$s you are following.

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

Answer by SkaredCreations · May 28, 2020 at 10:17 AM

Add the method OnCollisionEnter (or OnTriggerEnter if your Collider is set as trigger) and call TakeDamage inside it, also you may want to check collision.gameObject.tag inside it to filter only the collisions with enemy gameobjects else you'll take damage also colliding with walls etc.

Also I see that you're checking whether currentHealth reaches 0 inside your Update, that's not necessary since the only line of code where you decreases currentHealth is in TakeDamage so I'd move that statement inside TakeDamage after the call to healthBar.SetHealth

A better approach to your TakeDamage:

 void TakeDamage(int damage)
 {
      // The following line ensures that currentHealth is minimum 0 (avoid negative number)
      currentHealth = Mathf.Max(0, currentHealth - damage);
      healthBar.SetHealth(currentHealth);
      if (currentHealth == 0)
           Destroy(gameObject);
 }



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 ilgazalsancak2006 · May 28, 2020 at 04:36 PM 0
Share

I'm a beginner in Unity so I'm still learning to write code by myself. Can you tell me what I need to add to this script? The player still only takes damage while jumping. I want him to take damage when hitting an enemy. Script:

private void OnCollisionEnter2D() { TakeDamage(10); }

Thanks for the 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

127 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

Related Questions

BoxCollider2D not triggering 1 Answer

Looking for a clean bug-free solution to my issue with collisions on a moving UI object with other UI objects 1 Answer

How can I avoid collisions between the same objects? 3 Answers

Changing layers on runtime does not work 1 Answer

2D collision matrix does not work in unity 2020 4 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