aFilthy-Casual

xp % test

Nov 15th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. public class player_xp_manager : Monobehaviour
  6. {
  7.     [Header("UI Elements")]
  8.     public Slider xpBar; // slider used to display current xp
  9.     public Text displayText; // text box to display % in
  10.  
  11.     [HideInInSpector] // hidden in inspector so you can't accidentally add values to this variable
  12.     public float currentXp; // current xp the player has in this "level" - set to public so can be edited from other scripts
  13.     private float levelUpThreshold; // xp that is needed to level up   
  14.     [ToolTip("This Float is How much the level up threshold increases with each level")]
  15.     public float xpThresholdIncrease; // amount to increase levelup threshold on level up
  16.     private float levelProgress_percentage; // xp progress percentage
  17.     void start()
  18.     {
  19.        
  20.     }
  21.  
  22.     void Update()
  23.     {
  24.         levelProgress_percentage = (currentXp / levelUpThreshold) * 100;
  25.         xpBar.Value = currentXp;
  26.         displayText.text = levelProgress_percentage.ToString("00") + "%";
  27. // if you want to include decimals the have ToString("00.00") each '0' after '.' represents another decimal place
  28.  
  29.         if (currentXp >= levelUpThreshold)
  30.         {
  31.             onLevelUp();
  32.             // call levelup method on your player manager script
  33.         }
  34.     }
  35.     void onLevelUp()
  36.     {
  37.         levelUpThreshold += xpThresholdIncrease;
  38.         currentXp = 0;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment