Question3r
Question3r

Reputation: 3832

Building up a robust experience system in Unity

I have a simple experience system in Unity.

    public void GainExperience(float experienceToAdd) // the Player gains some XP
    {
        currentExperience += experienceToAdd; // add the XP to the current XP

        if (currentExperience > experienceNeeded) // enough XP to level up
        {
            currentExperience = currentExperience - experienceNeeded; // reset the current XP
            experienceNeededToLevelUp *= 2; // increase the needed xp for the next level
            currentLevel++; // increase the players level
            skillpoints++; // gain a skillpoint
            UpdateLevelText(); // GUI Update
        }

        UpdateXPBar(); // GUI Update
    }

So I think this code is not the best. When I get that much XP, that I can level up twice or more, the code will not be correct.

So how can I modify it?

Upvotes: 1

Views: 1827

Answers (2)

FLX
FLX

Reputation: 2679

You could use an editor CurveField to get a custom progression.

enter image description here

Then in GainExperience() you would just have to use Evaluate() with XP as parameter to get the new level, and if it's different from the current level calling your update GUI functions.

Upvotes: 1

Michael Hagar
Michael Hagar

Reputation: 646

while (currentExperience >= experienceNeeded)
{
    currentLevel++;
    skillpoints++;

    currentExperience -= experienceNeeded;
    experienceNeeded *= 2;
}

Upvotes: 5

Related Questions