- Home /
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 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;
}
You can use "ClipArea" shader for creating a progress bar an easy solution.
@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....
@Hamdullahshah can you write any code or any example ... and can we make it appear or disappear any time we want ? .. kindly help :(
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;
}
@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 .. :) ...
I edited the code above. It now uses two textures. Text2 is the background texture.
@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 ..
@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.
@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)...
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Create big city for car game? 2 Answers
Tracking Down GUI Errors 0 Answers