Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by davidflynn2 · Aug 14, 2013 at 06:40 PM · c#leveling

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); 
         
         
     
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pitimoi · Aug 14, 2013 at 07:16 PM 2
Share

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...

avatar image davidflynn2 · Aug 14, 2013 at 07:34 PM 0
Share

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.

avatar image ShadoX · Aug 15, 2013 at 06:53 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.alt text

 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());
     }
 }


g.png (158 B)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by ShadoX · Aug 14, 2013 at 08:03 PM

btw: I believe that you can get the screen width with

 Screen.width
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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 heredouble 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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Case structor in C# Unity 1 Answer

new user very easy c# question regarding syntax 1 Answer

Load Level Help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges