- Home /
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
I'm not trying to get things for free you can just give an explanation on how to calculate, I'm not telling you "HEY GIVE $$anonymous$$E A SCRIPT FOR WHAT I NEED" so forgive me for expressing myself poorly maby it's was my mistake to explain like that but you dont have to comment if you don like
did you check this question: http://answers.unity3d.com/questions/125359/rpg-algorithms.html
Answer by RyanZimmerman87 · Feb 02, 2014 at 01:57 AM
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);
GUI.EndGroup();
GUI.EndGroup();
//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.
well tnhx for the explanation but the visual part I got it solved i was attendig more for the logic xD maby I miss explain but thanx
Oh dangit, was worried that might be the case.
Do you have a specific problem with the logic? Is their something you don't understand how to do with the program$$anonymous$$g?
This just really seems like one of those things that you would benefit a lot more from by solving yourself then trying to copy someone. Setting up your leveling system is a good mental exercise and good practice if you're new to program$$anonymous$$g.
It really shouldn't be too overly difficult in the grand scheme of game program$$anonymous$$g but if you have a specific question I could try to help.
Thanx I'll keep that in $$anonymous$$d but before I will try to mae it my seft xP