Level System vs GUI

Hi guys i hv my level system work perfectly and now i want to representate the level exp in Game with GUI, the bar rise until max and turn back to 0 when i level up like diablo, but right know the back groud bar is there but the exp bar is not going up… can anyone help me plz? i hv other problem the bar is not resize with screen resolution right now. wt im missing here too?

Apreciate ur help

using UnityEngine;
using System.Collections;

public class LevelSystem : MonoBehaviour
{
    public int level;
    public int exp;
    public Fighter player;

    public Texture2D expback;
    public Rect expbackPosition;

    public float horizontalDistance;
    public float verticalDistance;
    public float width;
    public float height;

    public Texture2D expfull;
    public Rect expfullPosition;

    public float expPercentage;

    // Use this for initialization
    void Start ()
    {
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        LevelUp ();
        expPercentage = (float)exp / (Mathf.Pow(level, 3)+1000);
    }

    void LevelUp()
    {
        if(exp>=Mathf.Pow(level, 3)+1000)                        //exponencial formula = (level*level*level)+1000
        {
            exp = exp - (int)(Mathf.Pow(level, 3)+1000);        //put(int) to receive int value cuz the Mathf function will retunr float value
            level = level + 1;
            LevelEffect();
        }
    }

    void LevelEffect()
    {
        //Assing all the effects that lvl up give to player status
        //level power 3 to status:
        //player.damage = player.damage + Mathf.Pow(level, 3);
        player.maxHealth = player.maxHealth + 100;                //each lvl up give more 100 health points
        player.damage = player.damage + 50;                        //each lvl up give more 50 damage points
        player.health = player.maxHealth;
    }

    void OnGUI()
    {
        drawexpback();
        drawexpfull();
    }

    void drawexpback()
    {

        expbackPosition.x = (Screen.width - expbackPosition.width) / 6;
        expbackPosition.width = Screen.width * width;
        expbackPosition.height = Screen.height * height;
        GUI.DrawTexture (expbackPosition, expback);

    }

    void drawexpfull()
    {

        expfullPosition.x = expbackPosition.x + expbackPosition.width * horizontalDistance;
        expfullPosition.y = expbackPosition.y + expbackPosition.height * verticalDistance;
        expfullPosition.width = expbackPosition.width * width * expPercentage;
        expfullPosition.height = expbackPosition.height * height;
        GUI.DrawTexture (expfullPosition, expfull);
    }
}

About resize the bar, its resize when we change the resolution, but wt doesnt happen is when we start game in maximaze on Play the bar Rise in Y axes to about half of screen… But if i play in non Maximize on Play and change for diferente resolution it work…

About the exp part i keep it dont working for now…

All help will be appreciated

After i look this for much time i think the problem is my expPercentage equation… since it should be exp(currente exp) / exp need for lvl up and i use this:

expPercentage = (float)exp / (Mathf.Pow(level, 3)+1000);

where: (float)exp / (Mathf.Pow(level, 3)+1000) is the formula for exp needed to level up but i think i cant use this to define the max exp i need in current level and use it in expPercentage… and then my xp bar dont work…

So can anyone help me with this plz. Appreciate it

After i made a few more tests i realize that the expPercentage is working fine cuz, for example to rise level 1 to 2 i need 1001xp. I hv to enemys, when i kill the 1st one i receive 600xp and in inspector indicate 0.599% of exp need to rise level, and when i kill the 2nd one that give 500xp i rise to level 2 and i hv now 0.09xp of lvl 2 cuz now i need 1008xp to rise level 3. So everything is working fine the only problem is that the xp bar is not drawing. Only draws the background bar…

I will keep w8 some kind help cuz for sure this is one little think that im missing for all day long :s

Appreciate

Anyone plz? I lose all this looking to this script and im going crazy lol. I already change it like 20 times. Plz if someone can help me i will appreciate a lot… Thx

As u can see in this image… in the inspector, im receiving experience, and the exp percentage is working too, since i need 1001 to rise next level with the mob i kill i win 600 xp so 0.599% exp percentage… So the only problem here only can be in this part of the code… cuz it isnt drawing the bar when i gain XP…

:confused: just plz anyone can help me with this

void drawexpfull()
    {
        expfullPosition.x = expbackPosition.x + expbackPosition.width * horizontalDistance;
        expfullPosition.y = expbackPosition.y + expbackPosition.height * verticalDistance;
        expfullPosition.width = expbackPosition.width * width * expPercentage;
        expfullPosition.height = expbackPosition.height * height;
        GUI.DrawTexture (expfullPosition, expfull);
    }

Why are you multiplying by width? Unless I missed something, width is never assigned. If its value is 0, then expFullPosition.width will be 0.

Do you need to change this:

expfullPosition.width = expbackPosition.width * width * expPercentage;

to this?

expfullPosition.width = expbackPosition.width * expPercentage;
1 Like

Probably thats the problem… I know that probably be a little stupid thing… I will test when im home and tell u if work. Ty m8

Ok even changing that it doesnt draw the xp bar. As u can see in next image i win xp from a mob (exp: 600) that correspondes to 0.59 of expPercentage, so the level calculation is working fine… the background image of XP Bar is draw at grey but the yellow one who is the real XP bar dont grow up with xp i win from mobs…

This is really annoying me…

And the code is this one now:

using UnityEngine;
using System.Collections;

public class LevelSystem : MonoBehaviour
{
    public int level;
    public int exp;
    public Fighter player;
   
    public Texture2D expback;
    public Rect expbackPosition;
   
    public float horizontalDistance;
    public float verticalDistance;
    public float width;
    public float height;
   
    public Texture2D expfull;
    public Rect expfullPosition;
   
    public float expPercentage;
   
    // Use this for initialization
    void Start ()
    {
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        LevelUp ();
        expPercentage = (float)exp / (Mathf.Pow(level, 3)+1000);
    }
   
    void LevelUp()
    {
        if(exp>=Mathf.Pow(level, 3)+1000)                        //exponencial formula = (level*level*level)+1000
        {
            exp = exp - (int)(Mathf.Pow(level, 3)+1000);        //put(int) to receive int value cuz the Mathf function will retunr float value
            level = level + 1;
            LevelEffect();
        }
    }
   
    void LevelEffect()
    {
        //Assing all the effects that lvl up give to player status
        //level power 3 to status:
        //player.damage = player.damage + Mathf.Pow(level, 3);
        player.maxHealth = player.maxHealth + 100;                //each lvl up give more 100 health points
        player.damage = player.damage + 50;                        //each lvl up give more 50 damage points
        player.health = player.maxHealth;
    }
   
    void OnGUI()
    {
        drawexpback();
        drawexpfull();
    }
   
    void drawexpback()
    {
       
        expbackPosition.x = (Screen.width - expbackPosition.width) / 6;
        expbackPosition.width = Screen.width * width;
        expbackPosition.height = Screen.height * height;
        GUI.DrawTexture (expbackPosition, expback);
       
    }
   
    void drawexpfull()
    {
       
        expfullPosition.x = expbackPosition.x + expbackPosition.width * horizontalDistance;
        expfullPosition.y = expbackPosition.y + expbackPosition.height * verticalDistance;
        expfullPosition.width = expbackPosition.width * expPercentage;
        expfullPosition.height = expbackPosition.height * height;
        GUI.DrawTexture (expfullPosition, expfull);
    }
}

I See now the exp bar parameters hv something wrong… this parameters appear automaticly but if we check the X and Y values that corresponde to grey background bar ones, but the W and H hv nothing to do in comparation with the grey bar… seems the bar is drawing in other place…

NVM the exp bar is drawing but the Height is just too small… and only draw at little top of background bar… just need fix the height of exp bar