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