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 Instinct · Dec 08, 2014 at 12:43 PM · intpercentagehealth

Call a function based on percentage of health (int)

Hello all!

I am wondering what the best way to run a function based on health percentage.
Example: If enemy is below 70% health, do a cool mechanic.

I have designed the mechanic on my mega boss character, but the way I trigger the mechanic (function) is kind of awful in my eyes. This is how I do it:

 //function is updated each time the enemy takes damage
 public void DamageEnemy(float damage){ 
 
  currentHealth -= damage; //HP goes down as the enemy takes damage
  
  if(isMegaBoss){ //check if enemy is a mega boss
      if(currentHealth / health <= 0.9f && currentHealth / health >= 0.85f)
                  SendInReinforcement();
      else if(currentHealth / health <= 0.7f && currentHealth / health >= 0.65f)
                  SendInReinforcement();
      else if(currentHealth / health <= 0.5f && currentHealth / health >= 0.45f)
                  SendInReinforcement();
  } 
 }


So as you see, I want to trigger this mechanic each time the boss reaches 90%, 70%, 50% of his health. But there has to be a better way to check this? I'm bad at math, that's probably my problem :D

In advance, thanks for the help!

Cheers, Simon

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
3
Best Answer

Answer by NickvanDokkum · Dec 08, 2014 at 01:02 PM

What is the health variable? maybe use another variable for knowing what reinforcements you've already send?

 private int sentReinforcements = 0;
 if(isMegaBoss){
       if(currentHealth / health <= 0.9f && sentReinforcements == 0)
                   SendInReinforcement();
                   sentReinforcements ++;
       else if(currentHealth / health <= 0.7f && sentReinforcements == 1) 
                   SendInReinforcement();
                   sentReinforcements ++;
       else if(currentHealth / health <= 0.5f && sentReinforcements == 2)
                   SendInReinforcement();
                   sentReinforcements ++;
   } 

other then that i can't think of anything else to clean your code up. (and it'll fix if he sends out reinforcements twice while inbetween 0.9 and 0.85 health for example)

Comment
Add comment · Show 3 · 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 Instinct · Dec 08, 2014 at 01:14 PM 0
Share

The max health var is: health, the currenthealth variable is: currentHealth. There isn't any problem with my code. The reinforcement is sent out correctly etc. (Using an "RunOnce" boolean to check that it wont run multiple times),
I just don't like how the code is setup.

But I might use the "sentReinforcements ++;" check ins$$anonymous$$d. Thanks for the additional tip! :)

avatar image Instinct · Dec 08, 2014 at 01:44 PM 0
Share

OOOhhh, I am quite sorry. I read your code too fast, and overlooked your answer. You got the solution, this is what I wanted! Thanks! Just ignore my previous answer, hehe.

avatar image Instinct · Dec 08, 2014 at 01:48 PM 0
Share

Result, using both answers in this thread (thanks to both of you):

 if(is$$anonymous$$egaBoss){
             if((currentHealthPercentage <= 0.9f && ReinforcementSent == 0) || (currentHealthPercentage <= 0.7f && ReinforcementSent == 1) || 
                (currentHealthPercentage <= 0.5f && ReinforcementSent == 2) || (currentHealthPercentage <= 0.3f && ReinforcementSent == 3)){
                 gameControllerScript.megaBossController.StartReinforcementSequence();
                 ReinforcementSent++;
             }
avatar image
1

Answer by instruct9r · Dec 08, 2014 at 01:03 PM

Well. If you want to check for 2 options in the if statement the way you wrote it looks good to me..

you can just make one variable in the DamageEnemy function called (For example) healthAfterDamage and then do

 healthAfterDamage = currentHealth / health;

Then check that variable. This way you will divide only once...

Additionally you can do all of the if's in one statement, but it will be kinda long..

 if (healthAfterDamage <= 0.9f && healthAfterDamage >= 0.85f || currentHealth / health <= 0.7f && currentHealth / health >= 0.65f || and so on).










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 Instinct · Dec 08, 2014 at 01:18 PM 0
Share

It sure gets cleaner with your code, I'll do this and maybe not so resource-heavy. Thanks!

I'll try to tinker with the code, see what I come up with!

avatar image Instinct · Dec 08, 2014 at 01:48 PM 0
Share

Result, using both answers in this thread (thanks to both of you):

 if(is$$anonymous$$egaBoss){
             if((currentHealthPercentage <= 0.9f && ReinforcementSent == 0) || (currentHealthPercentage <= 0.7f && ReinforcementSent == 1) || 
                (currentHealthPercentage <= 0.5f && ReinforcementSent == 2) || (currentHealthPercentage <= 0.3f && ReinforcementSent == 3)){
                 gameControllerScript.megaBossController.StartReinforcementSequence();
                 ReinforcementSent++;
             }

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

27 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

Related Questions

Health Bar To Change Size 1 Answer

What's the best way to create a healthbar which represents the percentage of health the player has left? 2 Answers

Major int interacting with lesser float 1 Answer

Health bar by percentage? (not drawing a box) 4 Answers

Zombie won't die 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