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 Omer.Hussain · Feb 26, 2014 at 01:54 PM · guihealthbarprogress-barpower-up

Power up timer...

hi .. i have made my power up part, when i collide with my power up object magnet, it starts collecting coins(like magnet catcher) and after 12 seconds it backs to normal. what i am looking for is just to show a power up bar which like health bar which show for the time we assign, like 12 seconds and after that disappear. ... like in subway surfer,like this alt text or minnion rush circle power up bar ... i have searched but i am not understanding it very well ... i have tried to applied this one .. but its not working like i want :( ... kindly help me please ... i ll be more than glade .. :'( .. its driving me crazy ..

 #pragma strict
 
     var barDisplay : float = 0;
     internal var pos : Vector2 = new Vector2(20,40);
     internal var size : Vector2 = new Vector2(20,50);
     var EmptyS: GUIStyle;
     var FullS: GUIStyle;
     
     var progressBarEmpty : Texture2D;
     var progressBarFull : Texture2D;
     var progressBar : boolean = false;
          
     function OnGUI()
     {
     if (progressBar==true)
     { 
     // 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 * barDisplay, size.y));
     GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
     GUI.EndGroup ();
      
     GUI.EndGroup ();
      
         }
     }
      
     function Update()
     {
     // for this example, the bar display is linked to the current time,
     // however you would set this value based on your desired display
     // eg, the loading progress, the player's health, or whatever.
     barDisplay = 1 -Time.time* 0.05;
     }
     
 


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 Graham-Dunnett ♦♦ · Feb 26, 2014 at 01:55 PM 0
Share

What does "it's not working like I want" mean?

avatar image Hamdullahshah · Feb 26, 2014 at 02:02 PM 0
Share

You can use "ClipArea" shader for creating a progress bar an easy solution.

avatar image Omer.Hussain · Feb 27, 2014 at 05:52 AM 0
Share

@Graham i am trying to make timer bar which show for the time we assign as if we assign 6 second or 12 seconds... kindly check my code it doesn't work like the timer ... it doesn't disappear after the bar get fill out and i couldn't make the timer with it as we have in the subway surfer. it doesn't change the size in the x and if i remove the style then the box borders show and we can manage size as we want in x and y....

avatar image Omer.Hussain · Feb 27, 2014 at 05:55 AM 0
Share

@Hamdullahshah can you write any code or any example ... and can we make it appear or disappear any time we want ? .. kindly help :(

1 Reply

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

Answer by robertbu · Feb 27, 2014 at 07:31 AM

Here is a bit of code for one way of doing this behavior. Just something to get you started since I suspect you are visualizing something specific. Start a new scene, attach the following script to an empty game object, drag a texture you want to reveal over time to the 'tex' variable, hit play, and click the mouse button.

 #pragma strict
  
 var tex  : Texture;
 var tex2 : Texture;
  
 var percent = 1.0;
 var showPowerUp = false;
  
 function Update() {
     if (!showPowerUp && Input.GetMouseButtonDown(0)) {
        PowerUpOverTime(10.0);
     }
 }
  
 function OnGUI() {
     if (showPowerUp) {
        GUI.DrawTexture(Rect(0,0, tex.width, tex.height), tex2);
        GUI.DrawTextureWithTexCoords(Rect(0,0, tex.width * (1.0 - percent), tex.height), tex, Rect(0,0,1.0 - percent,1));
     }
 }
  
 function PowerUpOverTime(time : float) {
     var timer = 0.0;
     showPowerUp = true;
     while (timer < time) {
        percent = timer / time;
        timer += Time.deltaTime;
        yield;
     }
    showPowerUp = false;
 }
Comment
Add comment · Show 7 · 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 Omer.Hussain · Mar 03, 2014 at 11:07 AM 0
Share

@robertbu its really good .. it disappear after 10sec ... great .. one thing more the bar goes from left to right .. in power ups it goes from right to left and how i ll change its size because i need to show it on the left down corner... and need to show an image with it , like in the above example it show 2x and power up goes from right to left ... Thanks Regards .. :) ...

avatar image robertbu · Mar 03, 2014 at 12:40 PM 0
Share

I edited the code above. It now uses two textures. Text2 is the background texture.

avatar image Omer.Hussain · Mar 03, 2014 at 02:31 PM 0
Share

@robertbu yah its working :').... :p ... only thing remain is its size changing and the picture to attach ...size covers the full screen ... not on the left down corner .. and piture .. plz its the last two work remaining ..

avatar image robertbu · Mar 03, 2014 at 05:21 PM 0
Share

@Omer Hussian - you need to take over here. The size and position is deter$$anonymous$$ed by what you pass to the GUI.DrawTexture() and the GUI.TextureWithTexCoords(). The first two parameters of the Rect deter$$anonymous$$e the upper left corner of placement. If you want something that goes the whole screen, make a texture that goes the whole screen or you can fiddle with a multiplier wherever you see 'tex.width'. As for the picture, I'm not an artist. I'm a programmer. When I tested the code to make sure it was working, I clipped a screen shot from Subway surfer, but that is copyrighted, so for your own game you are going to need to create your own image.

avatar image Omer.Hussain · Mar 04, 2014 at 08:20 AM 0
Share

@robertbu i changed code like this: GUI.DrawTexture(Rect(0,0, 130, 25), tex2); GUI.DrawTextureWithTexCoords(Rect(0,0, 130 * (1.0 - percent), 25), tex, Rect(0,0,1.0 - percent,1)); now the bar shows on the left upper corner , i will make it down as well on the left.. thanks .. and about picture i didnt ask for the picture to draw, i just ask how i will place that left corner picture :) .. and i think now in this code we use two images one for background and one for forground, if i draw a forground image transparent from the inner side and draw borders outer side and on the left corner with image it will work well ... ..or we could show GUI texture ... am i going right..? or is it just show textures not images (this code)...

Show more comments

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

21 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 avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Create big city for car game? 2 Answers

Tracking Down GUI Errors 0 Answers

How to make a progress bar(non uniform shape) 2 Answers

C# Applying Transparency to a Single GUI.Button 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