- Home /
newbie question how would get the percentage of its original starting value over its current value
somthing like this?
public int HP=1000;//current//this one goes down when reciving damage
public int HP_Max=1000;//maximum health where it started of
if(current HP is divisible within within the range of 100% to 80% of its Max HP){ Something();//no danger
}
else if(current health is divisible within within the range of 80% to 60% of its Max HP){ something();//warning
}
Answer by ByteSheep · Dec 19, 2013 at 05:52 AM
Here's a forum thread on the subject.
You can either use if statements to check if the health value is between two given min/max values, or you can use the Mathf.Clamp function to create a temporary value between a given range and check if it still matches your original value (in which case it is within range).
Mathf.Clamp solution:
public int HP=100;
public int HP_Max=100;
public int HP_Min=80;
// check if HP value is between 80% to 100%
if (Mathf.Clamp(HP, HP_Min, HP_Max) == HP)
{
// HP value is between 80% to 100%
// do something
}
And the alternative using if statements:
if (HP <= HP_Max && HP >= HP_Min)
{
//HP value is within range
}
thanks a lot! i will try it out. here is what i was doing before i received your answer
public int HP=1000;
public int HP_$$anonymous$$ax=1000;
if(3/4 * HP_$$anonymous$$ax<HP && HP_$$anonymous$$ax>HP){//within 75% and 100%
}
i tried using fractions to see if it works and it also did work
Yes you are essentially doing the same thing as in the second example above, except you are specifying the $$anonymous$$ value directly in the if statement (in this case 3/4).
All these will work fine, there may be some slight differences in performance and readability, but so long as your code doesn't get too cluttered this approach should be fine.
i kinda i'm not a fan having to focus on too many outside variables that's why i'm trying to reduce public variables usage i'm guilty of it and do it inside the process ins$$anonymous$$d or have more control like your first and second example or not... but will a process be more of an heavy operation than a stored global variable. is the CG triggered, but any way that's totally out of my question now
Your answer
![](https://koobas.hobune.stream/wayback/20220613124449im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation + Attack when in range 1 Answer
Is there a way to define a range of numbers in the place where an int would usually go? 2 Answers
Ai that applies damage when in range? 1 Answer
My integer for my player health seems to be only returned once 2 Answers
Calling a variable from another class 2 Answers