Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- public class player_xp_manager : Monobehaviour
- {
- [Header("UI Elements")]
- public Slider xpBar; // slider used to display current xp
- public Text displayText; // text box to display % in
- [HideInInSpector] // hidden in inspector so you can't accidentally add values to this variable
- public float currentXp; // current xp the player has in this "level" - set to public so can be edited from other scripts
- private float levelUpThreshold; // xp that is needed to level up
- [ToolTip("This Float is How much the level up threshold increases with each level")]
- public float xpThresholdIncrease; // amount to increase levelup threshold on level up
- private float levelProgress_percentage; // xp progress percentage
- void start()
- {
- }
- void Update()
- {
- levelProgress_percentage = (currentXp / levelUpThreshold) * 100;
- xpBar.Value = currentXp;
- displayText.text = levelProgress_percentage.ToString("00") + "%";
- // if you want to include decimals the have ToString("00.00") each '0' after '.' represents another decimal place
- if (currentXp >= levelUpThreshold)
- {
- onLevelUp();
- // call levelup method on your player manager script
- }
- }
- void onLevelUp()
- {
- levelUpThreshold += xpThresholdIncrease;
- currentXp = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment