Progress bar until next shot can be fired

I just finished the space ship/shooter tutorial, and i added a secondary fire that clears the screen, it has a delay time of eight seconds inbetween shots and i wanted to make a progress bar that shows when the next shot can be fired, ive already coded in a progress bar but i cant get the timing right
This is thew code that i have / am using

Edit: wfiretime and nextwfire are simply the timings fro the secondary firing, the same as the primary if youve seen the tutorial.

    public float barDisplay;
    public Vector2 pos = new Vector2(20,40);
    public Vector2 size = new Vector2(60,20);
    public Texture2D emptyTex;
    public Texture2D fullTex;

void OnGUI() {
        GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
        GUI.Box (new Rect (0, 0, size.x, size.y), emptyTex);

        GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
        GUI.Box (new Rect (0, 0, size.x, size.y), fullTex);
        GUI.EndGroup ();
        GUI.EndGroup ();
    }

void Update()
    {
        barDisplay = (Time.time - wfiretime) / nextwfire;
}
1 Like

Your best using the new ui stuff, just create a canvas & set it to world space. Put a slider onto the canvas & you can set the value through code.

The math for working out the value to pass in is: reloadTimer/reloadTime

reloadTimer should count up to the reload time.

You will want to add “Using UnityEngine.UI;” to the top of a script to get access to the UI stuff & then you can have Slider variable :slight_smile:

1 Like

I mostly agree with the above, but instead of a slider I’d look at using an Image, and setting its fill amount. (https://docs.unity3d.com/ScriptReference/UI.Image-fillAmount.html)

1 Like