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 youngadultfriction · Jul 30, 2014 at 05:27 AM · beginnerbasicbarmeters

Trying to make a meter grow based on length of user input

Hi. I'm just starting out with Unity and I'm trying to start by making a simple program where the user simply has to hold down the space bar to make a meter fill up to the top and "win." I've gotten a script that will track whether the space bar is being held down, how long it has been held down for, and how fast the "chargeRate" of the bar should be. However, I'm not really sure how to actually make the bar fill up visually. I'm assuming it has to do with applying a 2D Vector and transform to the Sprite Renderer, but I have a limited understanding of how to actually make it so that the sprite (a simple green bar) will grow from nothing to 100%. Sorry if this is pretty basic! Most of the answers yielded scripts that were mainly for health bars that I could not get working.

EDIT: I managed to find this script, which let me create a functional bar and input my variables. However, when I press space, the bar loads from top to bottom, rather than bottom to top like I'd want. I updated my script to reflect these changes. Basically, my new problem is that I'm trying to flip the GUI.box that my meter is made out of so that it fills from bottom to top.

 #pragma strict
  
 var ap : float = 0; //action points meter; how long the player has held down space
 var chargeRate : float = 0.05; //the rate that the meter fills up; will vary based on level
 var charging = 0; //whether or not the player is holding down space to charge
 var spaceTime : float = 0;
  
 var barDisplay : float = 0;
 var pos : Vector2 = new Vector2(550,8);
 var size : Vector2 = new Vector2(66,368);
 var progressBarEmpty : Texture2D;
 var progressBarFull : Texture2D;
  
 function Start () {
  
 }
  
  
 function OnGUI()
 {
  
     // draw the background:
     GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
         GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
  
         // draw the filled-in part:
         GUI.BeginGroup (new Rect (0, 0, size.x, size.y * barDisplay));
             GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
         GUI.EndGroup ();
  
     GUI.EndGroup ();
  
 } 
  
 function Update () {
  
 if (Input.GetKey(KeyCode.Space))
 {
     charging = 1;
     Debug.Log("Charging is True!");
 }
 else
 {
     charging = 0;
     Debug.Log("Charging is False!");
 }
  
 if (charging==1)
 {
     spaceTime += Time.deltaTime;
     ap = (chargeRate * spaceTime);
     print("The variable is :"+ap);
 }
     barDisplay = ap;
  
 }


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by prof · Jul 30, 2014 at 08:24 AM

 GUI.BeginGroup (new Rect (0, size.y - (size.y * barDisplay), size.x, size.y));

line 27

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 youngadultfriction · Jul 30, 2014 at 09:04 AM 0
Share

Thanks! This is just what I needed! I tried using GUIUtility.RotateAroundPivot and it made positioning a nightmare. This is much easier.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need an in-depth explanation on this code. 2 Answers

Where to even begin? Character design, x-code integration, etc? 2 Answers

Help with Basic Movement Script 1 Answer

Basic javascript array declaration 2 Answers

BEGINNER: why is my main menu not loading level 1 when I press play? 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