Game Development Reference
In-Depth Information
First, we'll need to add a few variables; they'll be similar to the ones we used in the
health bar:
public float maxExperience = 100;
public float currentExperience = 0;
public float currentExpBarLength;
public float maxExpBarLength = 100;
Our next step will be to draw it in the OnGUI() function:
currentExpBarLength = currentExperience *
maxExpBarLength / maxExperience;
if(currentExpBarLength > 5)
GUI.Box(new Rect(Screen.width/2 - 20,
Screen.height/2 - 300, currentExpBarLength,
25), "");
GUI.Box(new Rect(Screen.width/2 - 20,
Screen.height/2 - 300, maxExperience, 25), "");
As you can see, we follow the same code as we did to draw our health bar, except
we draw two boxes on top of each other. The first box represents the current amount
of experience; it only shows when the player has earned more than five experiences.
This is to prevent the box from looking inside-out. The second box shows the max-
imum amount of experience.
Our next step will be to reset our experience and increase our level when the max-
imum amount of experience is gained. Enter this code:
if(currentExpBarLength >= maxExpBarLength)
{
currentExpBarLength = 0;
currentExperience = 0;
currentLevel++;
}
In the preceding if statement, we check to see whether our current experience bar
length is greater or equal to the maximum of the experience bar length. If it is, we
Search WWH ::




Custom Search