- Home /
Adding a bar that reset for each level
Ok I have with the help of those below managed to get my script cleaned up and an experience bar working but now at the last lvl (10) when you max your exp it still allows the experience to continue run the bar over the length over the length it should be I cant seem to get it to stop.
Here is the code.
using UnityEngine;
using System.Collections;
public class PlayerStats : MonoBehaviour
{
public int curExperience = 0;//Our current experience.
public int curLevel = 1;//Our current level.
playerHealth player;//Reference to the player health script and assigned to player so we can modify parts.
ShiledHealth shiledH;//Reference to the shiled health script and assigned to shiledH so we can modify parts.
Rect box = new Rect(10, 50, 160, 17);// Starte Cordonates
public Texture2D background; // Texture for the background of your health bar
public Texture2D foreground; // Texture for the forground of your health bar
public int[] healthlvl = new int [10] {400, 900, 1400, 2100, 2800, 3600, 4500, 5400, 6500, 7600};
public int[] shieldlvl = new int [10] {400, 900, 1400, 2100, 2800, 3600, 4500, 5400, 6500, 7600};
public int[] lvl = new int[10] {100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900};
void Start ()
{
player = GameObject.Find ("Player").GetComponent<playerHealth>();//This searches for the Gameobject you tell it to find the health script.
shiledH = GameObject.Find ("Player").GetComponent<ShiledHealth>();//This searches for the Gameobject you tell it to find the shield health script.
}
// Update is called once per frame
void Update ()
{
if(curLevel < lvl.Length && curExperience >= lvl[curLevel-1])
{
curLevel += 1;
player.playerMaxHealth += healthlvl[curLevel-1];
shiledH.shieldMaxHealth += shieldlvl[curLevel-1];
curExperience = 0;
}
}
void OnGUI()
{
GUI.DrawTexture(new Rect(148, 38, box.width, box.height), background, ScaleMode.StretchToFill);
GUI.DrawTexture(new Rect(148, 38, box.width*curExperience/lvl[curLevel-1], box.height), foreground, ScaleMode.StretchToFill);
}
}
Hi davidflynn2, You should probably began in scripting and just want to point something in your code (that's why I make a comment and not an answer) : In programmation, when you call variables something1, something2, etc... you could problably do a array ins$$anonymous$$d of 10 variables. For exemple :
public int lvl_1 = 400;
public int lvl_2 = 900;
public int lvl_3 = 1400;
public int lvl_4 = 2100;
public int lvl_5 = 2800;
public int lvl_6 = 3600;
public int lvl_7 = 4500;
public int lvl_8 = 5400;
public int lvl_9 = 6500;
public int lvl_10 = 7600;
can be :
public int[] lvl = new int[10] {400, 900, 1400, 2100, 2800, 3600, 4500, 5400, 6500, 7600};
Then, you can access each value using : lvl[0]
, lvl[1]
, etc...
With that method, your switch can be changed to :
if(curLevel < lvl.Length && curExperience >= lvl[curLevel-1])
{
curLevel += 1;
player.player$$anonymous$$axHealth += healthlvl[curLevel-1];
shiledH.shield$$anonymous$$axHealth += shieldlvl[curLevel-1];
}
Who is a 5 lignes script ins$$anonymous$$d of your current 75 lignes...
Wow awesome thanks so much I took a c# class in college last semester but I did not realize that it would work in here to do that. That makes a huge difference in my code thanks so much. Now I just got to get the GUI working then I am on the road.
The bars size keeps increasing most probably because you keep getting XP, which is used to calculate it's size at
GUI.DrawTexture(new Rect(148, 38, box.width*curExperience/lvl[curLevel-1], box.height), foreground, Scale$$anonymous$$ode.StretchToFill);
Either try preventing the player from getting any xp (which raises the value of curExpierience) or try addingsome check to make sure that it doesn't exceed the bars max. size.
for example, you could try replacing
box.width*curExperience/lvl[curLevel-1]
with
curLevel==maxLevel ? maxBarSize : box.width*curExperience/lvl[curLevel-1]
which is just a short if and is basically the same as
if(curLevel==maxLevel){
return maxBarSize; //which would be the max lengh of the bar
} else {
return box.width*curExperience/lvl[curLevel-1];
}
or you could add that in a function and just call it at that spot.
Answer by clunk47 · Aug 14, 2013 at 09:01 PM
This was in the script example I gave you in your other question. This makes the bar fill depending on percentages of max health and background length. Again, I've attached a sample image for you to use with this. You would need to add your current level / health variables obviously.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public Texture2D bar;
Rect bgRect;
Rect barRect;
Rect labelRect;
int exp = 0;
public int maxExp = 1000;
void Awake()
{
bgRect = new Rect(32, 32, 512, 24);
barRect = new Rect(34, 34, 0, 20);
labelRect = new Rect(32, 64, 128, 24);
}
void Update()
{
if(exp > 0)
barRect.width = exp * bgRect.width / maxExp;
//TEST
if(exp < maxExp)
{
if(Input.GetKey (KeyCode.KeypadPlus))
exp += 10;
}
}
void OnGUI()
{
GUI.Box (bgRect, GUIContent.none);
GUI.DrawTexture(barRect, bar);
GUI.Label(labelRect, exp.ToString());
}
}
Answer by ShadoX · Aug 14, 2013 at 08:03 PM
btw: I believe that you can get the screen width with
Screen.width
Answer by ShadoX · Aug 14, 2013 at 07:46 PM
If you know the proper length of the bar (not sure how that changes depending on screen resolution) you should be able to calculate how to increase the bars length and for how much xp.
Example:
int window_with = 640; //in this case you'd probably need to get this value from the resolution assuming that the resolution can be changed
//maximum bar size
double bar_width = 640*0.8;//so the bar would fill up 80% of the width and it's size would be 512
public int lvl_2 = 900;
double percet = lvl_2/bar_width; //how much xp is needed to fill up 1% of the bar
So all you have to do is to check the players XP and increase the bars size.
enter code here
double barSize = curExperience/percet; //which is the size of the bar on the screen
Additional you might improve the code if you would define the xp/hp/etc. for each level in a list/array (or even another object)
quick example with array/list:
int curLevel = 1;
int[] lvl_xp = {400,900,1400,2100,2800}//each entry represents 1 level
int[] lvl_hp = {10,20,30,40,50}
//so you could do all the checks/level ups via
if(curExperience >= lvl_xp[curLevel]){
player.playerMaxHealth += lvl_hp[curLevel];
curLevel += 1;
//...
}
This most probably isn't the best or pretiest way , but it might help or at last I hope so :)
Answer by pitimoi · Aug 14, 2013 at 08:13 PM
You can calculate you current progression with something like :
float percent = (float)curExperience / (float)lvl[curLevel];
Then, calcul the size of the second texture with :
int barSize = (int)Mathf.Lerp(0f, box.width, percent);
Finally, your second texture draw will be :
GUI.DrawTexture(new Rect(148, 38, barSize, box.height), foreground, ScaleMode.StretchToFill);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
new user very easy c# question regarding syntax 1 Answer
Distribute terrain in zones 3 Answers
Case structor in C# Unity 1 Answer
Fading To New Scene Problem? 1 Answer