Adding a fuel bar

Hi! I want to add a fuel bar to my game to make it more challenging. I’ve made it but I think my way of doing it is wrong because it feels like its draining fuel a lot faster than I want.

Here’s my fuel script :

public class BoostBar : MonoBehaviour

{

[SerializeField] Slider _boostSlider;

private float _minValue = 0;

private float _maxValue = 100;

private float _boost;

[SerializeField] float _boostUsage = 0.5f;

// Start is called before the first frame update

void Start()

{

    _boost = _maxValue;

    _boostSlider.value = _boost;

}

public void ConsumeBoost()

{

    _boost -= _boostUsage;

    _boostSlider.value = _boost;

}

}

Then I call ConsumeBoost() in the playerMovement script :

private void Boost()

{

    //Boost

    if (Input.GetKey(KeyCode.Space))

    {

        _rb.AddRelativeForce(Vector3.up * _thrusterPower * Time.deltaTime);

        BoostBar boostBar = GetComponent<BoostBar>();

        boostBar.ConsumeBoost();

        if (!_audioSource.isPlaying)

        {

            _audioSource.PlayOneShot(_boost, 0.5f);

            _mainBoost.Play();

        }

    }

    else

    {

        _mainBoost.Stop();

        _audioSource.Stop();

    }

}

}

I think as the player maintain the space bar down, it calls ConsumeBoost() a lot more than I want it to. How could I make this work more effectively?

Thank you

You are consuming 0.5f fuel per frame. That means you’ll be empty within 200 frames (with a max value of 100). You could multiply the value with Time.deltaTime which would then mean you are consuming 1 fuel in 2 seconds (0.5 per second). You can then just adjust the value to be 5 - for example - and you’ll use up all 100 fuel in 20 seconds. Or make it 10 to use it up in 10 seconds.

public void ConsumeBoost()
{
    _boost -= _boostUsage * Time.deltaTime;
    _boostSlider.value = _boost;
}

Hello! Thanks for the reply. I tried doing your suggestion by multiplying the value with Time.deltaTime and also changing the value to 5 or 10 and its a lot slower than before. But now its way too slow and it doesnt seem like changing the value (0.5, 5 or 10) changes anything. It seems to just be as slow wathever the value I put.

Yeah, those were just suggestions. I don’t know how fast you want the fuel to run out, that’s up to you and play testing. The number that goes into _boostUsage with this change is now how much fuel will be used per second. You need to decide how fast you want to have the fuel run out and adjust the value accordingly. Make it 100 and all the fuel is gone in 1 second. If it’s 1, it will take 100 seconds. if you make it 200, it will be half a second, etc.

1 Like

Where did you test the other values? With [SerializeField], the Inspector controls the _boostUsage variable. If you change 0.5f in the code, nothing will change because the exposed variable in the Inspector still has 0.5f.

2 Likes

Yes thank you that was exactly that. The value was unchanged in the inspector. Cant believe I forgot about that all this time :man_facepalming:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms