x


How to make an energy bar drain & fill based on holding a button

I am using a circular energy bar to display energy. Currently I have an alphamap that is filling & draining the texture for the desired results. My issue is that I cannot get it to fill back up when I let go from where it left off. Here is the incomplete code as I have it now:

public var energyTick = 0.1;

public var totalTick = 0.0;

function Update () {

if (Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift))

{

    totalTick = Time.deltaTime + totalTick  + energyTick;
       if (totalTick > 249){
       totalTick = 250;
       }
        var Energytotal = totalTick;
}
else
{
    Energytotal = Time.deltaTime - 1 - energyTick;
}

renderer.material.SetFloat("_Cutoff", Energytotal);

}

-------- I cant get the energy total or "current energy" to leave the first if statement so I can script it to refill. Essentially at full it starts as 0, at empty it is 250. I am trying to have it detect that shift is pressed then start draining it from 0 to 250, then when it hits 250 OR when shift is let go it then begins to fill back up. In my controller i have the shift key as a way to go from trot speed to run speed. I am essentially trying to limit its usage.

Long term I plan to have it detect character state, not shift key, and if character state is running then start the drain. When drain maxes out then character state becomes trotting. If character state is not running then begin the fill till it caps out from current to 0.

Any suggestions? Im new to C# but I would prefer to stick with it.

I am essentially adapting this HP bar to work as an energy bar but with different textures: http://answers.unity3d.com/questions/14770/creating-a-circular-progressbar-timer.html

more ▼

asked Jun 03 '11 at 07:15 AM

Quenadarious gravatar image

Quenadarious
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Try using

Energytotal -= Time.deltaTime - 1 - energyTick;

add the minus sign before equivalent.

more ▼

answered Jun 03 '11 at 07:32 AM

homeros gravatar image

homeros
321 1 2 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x69
x8

asked: Jun 03 '11 at 07:15 AM

Seen: 1283 times

Last Updated: Jun 03 '11 at 07:32 AM