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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by cariaga · Dec 19, 2013 at 05:01 AM · rangeintegerpercenthealth

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

}

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

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
 }


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 cariaga · Dec 19, 2013 at 06:06 AM 0
Share

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

avatar image ByteSheep · Dec 19, 2013 at 06:17 AM 1
Share

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.

avatar image cariaga · Dec 19, 2013 at 07:10 AM 0
Share

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

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

19 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

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


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