- Home /
Level and XP system
Hi,
I made an xp system with this tutorial: XP System
This is what i have:
public float XP;
public int CurrentLevel;
public float XpNextLevel;
public float differenceXp;
public float TotalDifferences;
public Text XpText;
public Image XpBar;
public Text Level;
private int LevelUp;
public IncreaseStats Stats;
public LevelUp Up;
public AudioSource LvlUp;
private void Start()
{
UpdateXp(0);
XpText.text = (TotalDifferences - differenceXp).ToString("0") + "/" + TotalDifferences.ToString("0");
}
public void UpdateXp(float xp)
{
XP += xp;
int curlvl = (int)(0.1f * Mathf.Sqrt(XP));
if(curlvl != CurrentLevel)
{
LevelUp = curlvl - CurrentLevel;
CurrentLevel = curlvl;
LvlUp.Play();
Stats.AddSkillPoints(LevelUp *2);
Up.Up();
}
XpNextLevel = 100 * (CurrentLevel + 1) * (CurrentLevel + 1);
differenceXp = XpNextLevel - XP;
TotalDifferences = XpNextLevel - (100 * CurrentLevel * CurrentLevel);
XpText.text = (TotalDifferences - differenceXp).ToString("0") + "/" + TotalDifferences.ToString("0");
XpBar.fillAmount = 1 / TotalDifferences * (TotalDifferences - differenceXp);
Level.text = (CurrentLevel + 1).ToString("0");
}
The thing is when i level up now, the first level is 100XP, then the next level is 300, and next 500 etc.
So it just goes up with 200XP every level i went up instead of in the video explained a nice curve.
What am i doing wrong?
Answer by blanktarget · May 24, 2018 at 05:40 PM
You need to have an exponent. I recently set this up in my game too.
Here is how I made a ramp of levels in an array to look at. In this case you can set your toLevelUp.Length to however many levels you want. An important thing to note here is that "i" starts at 1, because right now in my code at least level 0 is 0 xp so you instantly level up to 1. Just made things easier for how I coded the rest. Also, baseXP should be however much XP it takes to get to level 2. From there it goes up!
//XP ramp up
void LevelXPSetUp(){
for (int i = 1; i < toLevelUp.Length; i++) {
toLevelUp[i] = (int)(Mathf.Floor(baseXP*(Mathf.Pow(i,1.5f))));
}
}
I see you have an xp bar you want to fill up too on screen it looks like? I made it so mine fills over time too instead of jumping to the value. It looks nicer to see your xp bar fill instead of just suddenly being full.
This is what mine looks like:
//should count up until it hits the experience amount to add.
public IEnumerator AddExperience(int experienceToAdd){
addingXp = true;
//received from external sources. Add xp incrementally to move bar up slowly instead of chunks.
for (int i = 0; i < experienceToAdd; i++) {
currentXPAmount++;
yield return new WaitForSeconds (.002f);
}
addingXp = false;
}