Xp Calculation Bar

Hello well I have this experience system but it’s all right but when I get more xp than need the bar just get out of the screen and the level just keep going Up how do I write something that for example I have 200 xp and I need 700 for level up but I kill a monster that give me 1000 xp so I want to know how can I make when xp gets to 700 the xp bar goes to 0 and the restant xp adds to the bar so the bar will grow only the restant xp

One way to handle this it use OnGUI() to draw the bars for you. Here’s a quick example of the OnGUI() stuff:

//formatting style for OnGUI
public GUIStyle XPStyle;
//textures for empty and full xp bar
public Texture2D emptyXPTexture;
public Texture2D fullXPTexture;

//formatting vector
Vector3 matrixVector;

//position and size for texture bars	
Vector2 XPVectorPosition;
Vector2 XPVectorSize;

//formatting size example
float native_width = 1280; 
float native_height = 720;

//this float is the % of the visual bar being filled
float barDisplay;

//example variables
float playerCurrentXPRequired;
float playerLastXPRequired;

void Start() 
//prepare matrix variables
rx = Screen.width/ native_width;
ry = Screen.height / native_height;

//set matrix for OnGUI formatting
matrixVector = new Vector3 (rx, ry, 1);

//set the position and size for your xp bar
XPVectorPosition = new Vector2 (0, 0);
XPVectorSize = new Vector2 (500, 50);


void OnGUI()
//calculate the % of XP bar to be filled
barXPDisplay = (GlobalDataScript.playerXPTotal - playerLastXPRequired) / (playerCurrentXPRequired-playerLastXPRequired);

//formatting matrix
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, matrixVector); 
//draw the bars first empty than full
GUI.BeginGroup(new Rect(XPVectorPosition.x, XPVectorPosition.y, XPVectorSize.x, XPVectorSize.y),emptyXPTexture, XPStyle);
GUI.BeginGroup(new Rect(0, 0, XPVectorSize.x * barXPDisplay, XPVectorSize.y));
GUI.Box(new Rect(0, 0, XPVectorSize.x, XPVectorSize.y), fullXPTexture, XPStyle);

//can display text of how much xp you have and how much xp you need to level up
//adjust these position variables
GUI.Label (new Rect (0, 0, 0, 0), "" + GlobalDataScript.playerXPTotal + "/" + playerCurrentXPRequired
+ " XP", XPStyle);	

//whenever you gain XP call this function
public void XPUpdateFunction()
//code to determine if you leveled up when you gain xp
//code to set all the new variables if you leveled up

Apologies for the formatting, whenever I copy example stuff out of my current project it gets a bit hard to manage it to look perfect.

Hopefully that will get you started with displaying the bar visually.

I’m not sure if your question was mainly based on the bar visuals or the actual logic required to level up. But this should cover the visual stuff which is a bit more difficult if you are new to Unity.

I would recommend figuring out the leveling up logic yourself since it should be specifically designed for your game.

I try to name my variables so they are somewhat self explanatory but I don’t have time to outline what each one is exactly for the XP stuff, but hopefully this helps.