Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Divinitize1 · Sep 03, 2019 at 11:02 PM · mathxp

Could anyone help me with a bit of math for an XP system?

If my required XP to level up is 95/100 and I award 10xp, what's the best way to check what's leftover and put it in a variable that I can add after I level up?

Because at the moment if 95/100 and award 10, it will level up and go to 0 losing out on the 5. Sorry if it's a bit of a vague/mundane question, I struggle with maths.

 public float currentXP;
 public float RequiredXP;
 
 //If current is 95 and required is 100
 
   public void GiveXp(float xp)
   {
     //check if the given xp is higher than the required xp
     //And add the leftover into a variable so i can add on after
     //To the levelup function
   }

if I remember anything it may include the modulo % but I really struggle with math. Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Divinitize1 · Sep 03, 2019 at 11:57 PM

I'll mark this as correct as it works, however, it might not be the best way of doing things, see the below post for a one-line solution that almost works.

 public class xpSystem : MonoBehaviour
 {
   public float currentXP;
   public float xpRequired = 100f;
   public float totalXP;
   public int level;
   public GameObject xpBar;
 
   public float xpOverflow;
 
   void Update()
   {
     xpBar.GetComponent<UnityEngine.UI.Slider>().maxValue = xpRequired;
     xpBar.GetComponent<UnityEngine.UI.Slider>().value = currentXP;
     if(currentXP >= xpRequired)
     {
       LevelUp();
     }
     if(Input.GetKeyDown("space"))
     {
       GiveXp(15.7f);
     }
   }
   public void GiveXp(float xp)
   {
     if(currentXP + xp > xpRequired)
     {
       xpOverflow = currentXP + xp - xpRequired;
       Debug.Log("Leftover = " +  xpOverflow);
       currentXP += xp;
     }
     else
     {
       currentXP += xp;
     }
   }
 
   void LevelUp()
   {
     level += 1;
     xpRequired += 10;
     currentXP = 0f;
     currentXP += xpOverflow;
     xpOverflow = 0f;
   }
 }
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
2

Answer by Cornelis-de-Jager · Sep 03, 2019 at 11:08 PM

Here is a oneliner that does that for you

 public float currentXP;
 public float RequiredXP;

 public void GiveXp(float xp)
 {
     currentXP = currentXP + xp > RequiredXP ? (currentXP + xp) - RequiredXP : currentXP + xp;
 }
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
1

Answer by BastianUrbach · Sep 04, 2019 at 06:54 AM

Are you sure you're not overcomplicating things by first setting it to zero and then adding the overflow again? I would just do it like this:

currentXP += xp;
if (currentXP > RequiredXP) {
   currentXP -= RequiredXP;
   LevelUp();
}
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

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

119 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

Related Questions

xp math equation help 1 Answer

Problem with angles and rotations in a 2D top down space shooter 0 Answers

How can I get shader variable to map into a certain range? 0 Answers

Some math fun regarding scale and rotation 1 Answer

Calculating the perpendicular direction an AI ship needs to face to fire its cannons? 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