在Unity中构建强大的体验系统

时间:2017-08-08 21:48:56

标签: c# unity3d

我在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
    }

所以我觉得这段代码不是最好的。当我得到那么多XP,我可以升级两次或更多时,代码将不正确。

那我怎么修改呢?

2 个答案:

答案 0 :(得分:5)

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

    currentExperience -= experienceNeeded;
    experienceNeeded *= 2;
}

答案 1 :(得分:1)

您可以使用编辑器CurveField来获取自定义进度。

enter image description here

然后在GainExperience()中,您只需使用带有XP的Evaluate()作为参数来获得新级别,如果它与调用更新GUI功能的当前级别不同。