- Home /
Nitro bar setup
Hello. In my car game, I implemented nitro, when you apply it it multiplies the motortorque of the wheel colliders by two. But there's somethings I whant to do but i can't : I want to give the nitro a duration (for exemple, it lasts 4 seconds) and then i want it to take a time to recharge. I want to make a nitro bar associated with it (it shrinks as you use the nitro and grows as it recharges)
There is my nitro script :
*It is part of the car controller
void FixedUpdate()
{
NitroSystem();
}
void NitroSystem()
{
if(inputManager.nitro)
{
nitroIsOn = true;
}
else
{
nitroIsOn = false;
}
if(nitroIsOn == true)
{
nitroValue = 5f;
topSpeed = topSpeedValue * 2f;
}
else if (nitroIsOn == false)
{
nitroValue = 1f;
topSpeed = topSpeedValue;
}
}
Comment
Answer by Spip5 · Aug 15, 2020 at 02:06 PM
private float nitroAmount;
public float maxNitroAmount = 5;
public float nitroChargeSpeed;
public float nitroUseSpeed;
void Start()
{
NitroAmount = maxNitroAmount;
}
void NitroSystem()
{
if(inputManager.nitro)
{
nitroIsOn = true;
}
else
{
nitroIsOn = false;
}
if(nitroIsOn == true && nitroAmount > 0)
{
nitroAmount = Mathf.Clamp(nitroAmount - (Time.DeltaTime * nitroUseSpeed),0,nitroAmount);
topSpeed = topSpeedValue * 2f;
}
else if (nitroIsOn == false)
{
nitroAmount = Mathf.Clamp(nitroAmount + (Time.DeltaTime * nitroChargeSpeed),0,maxNitroAmount);
topSpeed = topSpeedValue;
}
}
And for the UI part, check this tutorial. logic for nitro is the same
If you need the bar to be fully recharged before using nitro again, I suggest using a coroutine with a timer. There are plenty of tutorials on how to do that