- Home /
How to automatically stop car when fuel ends up ?
So, I want to achieve a simple functionality here. On KeyCode.F pressed, the booster/fuel fills up and On KeyCode.W pressed (only once no need to hold) the car starts consuming booster/fuel. So far it's working good.
The Real problem I am facing is, to stop the car without pressing any key. I tried to add brakeTorque but i think that can't do this job. Any help will be appreciated..... (Sorry for bad english)
void Update()
{
if (Input.GetKey(KeyCode.F))
{
StartCoroutine(FillBooster());
}
if (Input.GetKeyDown(KeyCode.W))
{
isMoving = true;
StartCoroutine(ConsumeBooster());
}
}
IEnumerator ConsumeBooster()
{
while (!isBoosterEmpty())
{
boostBar.fillAmount -= Time.deltaTime/2;
SetMotorTorque(motorForce);
print("boost left : " + boostBar.fillAmount)
yield return null;
}
if (isBoosterEmpty())
{
ApplyBrake();
yield return null;
}
}
private void ApplyBrake()
{
print("is booster empty : " + isBoosterEmpty());
// here I tried to use brakeTorque but I think it doesn't work that way.
}
Answer by p3k07 · Oct 13, 2020 at 07:16 PM
Get rid of the while loop and just have
IEnumerator ConsumeBooster()
{
boostBar.fillAmount -= 0.1f;
SetMotorTorque(motorForce);
yield return new waitforseconds(2);
print("boost left : " + boostBar.fillAmount)
if(boostBar.fillAmount <= 0)
{ApplyBrake();}
}
Keep in mind you will need to start the CoRoutine again after 2 seconds, so I'd suggest just using a normal void and -= using delta time, as you already have done.
edit: Try this instead.
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
isMoving = true;
ConsumeBooster();
}
}
void ConsumeBooster()
{
if(boostBar.fillAmount > 0)
{
boostBar.fillAmount -= 2 * Time.deltaTime;
SetMotorTorque(motorForce);
print("boost left : " + boostBar.fillAmount)
}
else
{
ApplyBrake();
}
}
Thanks for your suggestion but I already mentioned I need help in stop the car automatically after the booster finishes up. And I'll consider your suggestions too, but I used while loop to get the smooth effect of getting the boostBar down and it's doing the work very effectively. Can you please suggest something to stop the car without pressing any key or something ?
This should get what you want. It's called once when you press W and will continue to -0.1 from the fill every 2 seconds until depleted. Once < 0 the while loop will break and the ApplyBrake method is called.
If you want it to subtract the fuel once per frame, then replace the WaitForSeconds with WaitUntilEndOfFrame.
Edit - Also, check out this https://docs.unity3d.com/ScriptReference/WaitWhile.html
IEnumerator ConsumeBooster()
{
while(boostBar.fillAmount > 0)
{
boostBar.fillAmount -= 0.1f;
Set$$anonymous$$otorTorque(motorForce);
yield return new waitforseconds(2);
print("boost left : " + boostBar.fillAmount)
// While loop will break when fill < 0, but just to make sure.
if(boostBar.fillAmount <= 0)
{ yield break;}
}
// This part of co-routine only accessed once when above while loop condition is false.
ApplyBrake();
}
Apply brake is being called correctly but I'm not able to stop the car. Because i do't know about motorToque and brakeTorque much. I tried to apply brakeTorque but it didn't work. So, i want some suggestion about what should go inside this method.
Answer by GrowingTurtle54 · Oct 13, 2020 at 07:18 PM
https://answers.unity.com/questions/1779420/how-do-i-test-my-multiplayer-game-with-friends.html Please help me with this
Your answer
Follow this Question
Related Questions
Strange Wheel Colliders 0 Answers
Wheel is wrong rotation 0 Answers
Problems with cars in Unity 5 0 Answers
Problem with cars in Unity 5 0 Answers
Does anyone know good free wheel collider alterantives? 0 Answers