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 Crisgar · Nov 09, 2020 at 10:05 PM · triggercharacterscrollbar

Problem with character Max Health and Healing items pick up.

Hello everyone, I made my first platform game and I was wondering if any of you could lend me a hand to fix some issues.

In the game the player Healthbar maximum capacity it's 1f and it is shown in the Scene with a Scrollbar.

It mostly works as it should, I can get damage from the enemies (bullets and contact) and I can heal myself 0,25f of health by picking up some healing items on the stages, the problem is that if I have full health the healing item will overheal me after going throught it (the healing item is destroyed afterwards). My idea was that whenever I have full health, my player won't be able to pick up the healing item and it will keep his health when going throught it, not destroying the item itself in the process. I'm currently using these two scripts:

This one for the healing item

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RecibirVida : MonoBehaviour
 
  {
     public float cura = 0.25f;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     void OnTriggerEnter(Collider other)
     {
         other.gameObject.GetComponent<HealthBar>().TakeLife(cura);
         Debug.Log("daño");
         Destroy(gameObject);
     }
 }

And this one for the player health.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 public class HealthBar : MonoBehaviour
 {
     public float MaxHealth = 1f;
     public float CurrentHealth;
     public Scrollbar Barra;
     bool damaged;
     bool Cura;
     bool isDead;
     CharacterController2D playerMovement;
     // Start is called before the first frame update
     
     void Start()
     {
         
         CurrentHealth = MaxHealth;
         
        // SetHelathBar();
     }
     public void TakeLife(float amount)
     {
         Cura = true;
         CurrentHealth += amount;
         Barra.size = CurrentHealth;
     }
     public void TakeDamage(float amount)
     {
         damaged = true;
         CurrentHealth -= amount;
         Barra.size = CurrentHealth;
         if (CurrentHealth <= 0 && !isDead)
         {
             //SceneManager.LoadScene("SampleScene");
             CurrentHealth = 0;
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
         }
         //SetHealthBar();
     }
     public void SetHealthBar()
     {
         float myHealth = CurrentHealth / MaxHealth;
         Barra.transform.localScale = new Vector3(Mathf.Clamp(myHealth, 0f,  1f), Barra.transform.localScale.y, Barra.transform.localScale.z);
     }
 }


Forgot to mention that some words are written in spanish, allow me to translate them: cura=heal, daño=damage, barra=bar (the name of the player health scrollbar)

And that's pretty much it, thanks beforehand for the help and my apologies if I posted it on the wrong section of the forum, first-timer here.

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

Answer by jackmw94 · Nov 09, 2020 at 11:51 PM

If there is some condition to whether or not TakeLife works then it'll have to return a flag saying whether or not it's completed successfully:

 public bool TakeLife(float amount)
 {
     float finalHealth = CurrentHealth + amount;
 
     if (finalHealth > 1)
     {
         return false;
     }
 
     Cura = true;
     CurrentHealth = finalHealth;
     Barra.size = CurrentHealth;
     return true;
 }


Then we can use that to determine whether or not we consume the health item:

 void OnTriggerEnter(Collider other)
 {
     bool completed = other.gameObject.GetComponent<HealthBar>().TakeLife(cura);
 
     if (completed)
     {
         Debug.Log("daño");
         Destroy(gameObject);
     }
     else
     {
         Debug.Log("Could not heal, current health too high");
     }
 }


If you change your mind and want to be able to pickup the item and limit health to one, you can use a 'clamp' function to set your health to stay between two values:

 public bool TakeLife(float amount)
 {
     Cura = true;
     CurrentHealth += finalHealth;
     CurrentHealth = Mathf.Clamp(CurrentHealth, 0, 1); 

     Barra.size = CurrentHealth;
     return true;
 }
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 jackmw94 · Nov 09, 2020 at 11:53 PM 0
Share

Also, as a side note: you can use Image components as health bars: if you assign a 'source image' to it then you'll see an option called 'image type' appear. By setting this to 'filled' you can then control how much of the image is filled via its 'fillAmount'. This will handle staying between 0 and 1 for you, you get the option of different fill method and you can give it your own image!

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

216 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Occasional taking control of Character Controller 1 Answer

Box Collider Trigger not working 1 Answer

Dialogue character emotions system 1 Answer

How can I stop killing myself ? 3 Answers

Cannot stop character movement upon trigger 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