Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Major · Oct 08, 2012 at 12:01 AM · boxlengthxpamount

Limit An XP Bar Length?

My question is almost completely what the title says. I am wondering how I would limit the length of my xp bar to a certain amount. The best example of what I want to do would be Mincraft's xp bar. Any ideas?

 static var Kills : int = 0;
 static var Money : int = 250;
 static var Lives : int = 10;
 var BasicTurret : Transform;
 var Paused : boolean = false;
 static var curExp : int;
 var maxExp : int = 50;
 var expTexture : Texture;
 var level : int;
 
 function LateUpdate()
 {
 }
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.P)&& !Paused)
     {
         Paused = true;
         Time.timeScale = 0;
     }
     
     else if(Input.GetKeyDown(KeyCode.P)&& Paused)
     {
         Paused = false;
         Time.timeScale = 1;
     }
     
     if(Input.GetKeyDown(KeyCode.Alpha1))
     {
         Time.timeScale = 1;
     }
     
     if(Input.GetKeyDown(KeyCode.Alpha2))
     {
         Time.timeScale = 2;
     }
     
     if(Input.GetKeyDown(KeyCode.Alpha3))
     {
         Time.timeScale = 3;
     }
     
     if(Lives <= 0)
     {
         Lives = 0;
         
         Loose();
     }
 
         if(curExp > maxExp)
     {
         curExp = 0;
         maxExp = maxExp * 1.5;
         level++;
     }
 }
 
 function OnGUI()
 {
     GUI.Box(Rect(Screen.width / 2 - 60, 10, 60, 20), "" + Kills);
     GUI.Box(Rect(Screen.width / 2, 10, 60, 20), "" + Lives);
     GUI.Box(Rect(10, 10 , 60, 20), "$" + Money);
     
     if(GUI.Button(Rect(10, 40, 40, 40), "Basic Turret"))
     {
         Money -= 350;
         
     }else{
     
     } 
     
     
     if(Paused)
     {
             
         if(GUI.Button(Rect(Screen.width / 2 - 30, Screen.height / 2 - 10, 60, 20), "Exit"))
         {
             Application.LoadLevel(0);
         }
         if(GUI.Button(Rect(Screen.width / 2 - 30, Screen.height / 2 + 20, 60, 20), "Quit"))
         {
             Application.Quit();
         }
     }
     
     GUI.Box(Rect(125, Screen.height - 35, curExp, 20), curExp + "/" + maxExp + " Level: " + level);
 }
 
 function Loose()
 {
     Application.LoadLevel(0);
     
     Time.timeScale = 1;
     
     Kills = 0;
     Money = 250;
     Lives = 10;
 }
Comment
Add comment · Show 4
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 rhys_vdw · Oct 08, 2012 at 01:37 AM 0
Share

Like a GUI element on the screen? Post what you've got so far.

avatar image Major · Oct 08, 2012 at 02:52 AM 0
Share

I have added the code up top.

avatar image williampigmeu · Oct 08, 2012 at 03:02 AM 0
Share

What is the maximum maxExp value? Just add an "if" comparing the atual "maxExp" value with "maxExpTotal" value.

avatar image Major · Oct 08, 2012 at 03:05 AM 0
Share

that is the total amount before the bar resets. but how do i limit how big the bar gets? and how do i make it so that while the total exp would be below that, it calculates how many pixels each exp amount would need to be to fill that space?

4 Replies

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

Answer by Major · Oct 09, 2012 at 02:58 AM

I found the answer to be very simple. All I had to do was edit line 87 to be this:

 GUI.Box(Rect(125, Screen.height - 35, (curExp*500)/maxExp, 20), curExp + "/" + maxExp + " Level: " + level);
Comment
Add comment · Show 1 · 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 rhys_vdw · Oct 09, 2012 at 03:16 AM 0
Share

That is just another way of writing the suggestions from myself and ausiemick. But I'm glad you understand the concept.

avatar image
0

Answer by williampigmeu · Oct 08, 2012 at 01:08 AM

There is some code "demo":

if (xpBarLength >= 10) { // "10" can be anything you want
 level += 1;
 xpBarLength = 0;
}

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 rhys_vdw · Oct 08, 2012 at 03:45 AM

You can "normalize" your XP and multiply it by a value, this puts it in a range between 0 and 1. something like this:

 var xp : float;
 var maxXp : float;
 var xpBarFullRect : Rect;
 
 function OnGUI() {
   var normalizedXp = xp / maxXp;
 
   var xpBarRect = xpBarFullRect;
   xpBarRect.width *= normalizedXp;
   GUI.Box(xpBarRect, xp + "/" + maxXp);
 }
Comment
Add comment · Show 2 · 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 Major · Oct 08, 2012 at 03:57 AM 0
Share

i tried that out but it didn't work.

avatar image rhys_vdw · Oct 08, 2012 at 04:12 AM 0
Share

Fixed an error.

I haven't tested it yet, but I hope you can follow my logic. If it's not clear I'll try to break it down for you.

avatar image
0

Answer by ausiemick · Oct 08, 2012 at 07:40 AM

Pretty much what you need todo is normalise the xp (normalisation is turning a range as such as 0 -> 40 to 0 -> 1...

the formula for that is:

 function normalise(minVal : float, maxVal : float, currValue : float)
 {
     return((currValue-minVal) / (maxVal-minVal))
 }

Please keep in mind that I never code in javascript + I coded this in a browser.. so I can almost guarentee my code won't work.. but if u can read you should be able to translate my code into javascript (if you had of asked for C# ide been able to write 100% bug free code :P) and also, I havn't tested this, this function is based purely on theory..

say we have a xp scale from 0 -> 100 and curr xp == 54 get currValue and take min value from it 54 - 0 = 54 get the max value and take the min value from it 100 - 0 = 100 then we divide the new curr value by the new max value

54/100

which gives us 0.54

Then what you do is define the screenspace for the box

a--------------------b

c--------------------d

then when you draw the box you would give it this:

 float normalisedXP = normalise(0, 100, 54);
 GUI.Box(Rect(a.x, a.y, (b-a)*normalisedXP, c-a), "" + XP);

where a, b, c, d are defined Vector2's who correlate to the size of how big you want the xp bar once it's finished

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

12 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

Related Questions

how make a for loop that makes GUI boxes ? 1 Answer

Amounts of lights 1 Answer

Internal collisions 1 Answer

Basic vector math addition 1 Answer

Create selection box by double tapping then dragging? (android mobile)(cnc style)(image embedded) 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