- Home /
The question is answered, right answer was accepted
Car game Reverse Button
Hello!
How do I create a button that, when pressed, makes the car go backwards?
My gas button works like this:
public void Gas ()
{
motorInputTouch = 1;
gasOn = true;
Debug.Log ("Gas");
wheelBR.brakeTorque = 0;
wheelBL.brakeTorque = 0;
// First gear
if (currentSpeed <= 150 && currentSpeed > -maxReverseSpeed && !braked) {
wheelBR.motorTorque = OneTorque * motorInputTouch;
wheelBL.motorTorque = OneTorque * motorInputTouch;
}
// Second gear
else if (currentSpeed <= 300 && currentSpeed > 150 && !braked) {
wheelBR.motorTorque = TwoTorque * motorInputTouch;
wheelBL.motorTorque = TwoTorque * motorInputTouch;
}
// Third gear
else if (currentSpeed <= 450 && currentSpeed > 300 && !braked) {
wheelBR.motorTorque = ThreeTorque * motorInputTouch;
wheelBL.motorTorque = ThreeTorque * motorInputTouch;
}
// Fourth gear
else if (currentSpeed > 450 && currentSpeed <= topSpeed && !braked) {
wheelBR.motorTorque = FourTorque * motorInputTouch;
wheelBL.motorTorque = FourTorque * motorInputTouch;
} else {
wheelBR.motorTorque = 0;
wheelBL.motorTorque = 0;
}
}
Answer by hexagonius · Feb 11, 2015 at 08:45 PM
I assume you call Gas() by pressing a button? You could create a second method called Backwards() getting called when you press another button.
If you want all that in your Gas() method, maybe you could wrap all your gear checks into a if (forward) boolean check. Then there's the else to do back traveling. Toggle that boolean upon pressing the button.
No I'd like to do a different button and call Reverse(), but I don't know how I can make the car go backwards.
Is it something like:
wheelBR.motorTorque = OneTorque * -motorInputTouch;
wheelBL.motorTorque = OneTorque * -motorInputTouch;
Never$$anonymous$$d forgot to add motorInputTouch = 1; !!!
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Reverse Button problem 0 Answers
Laggy Camera 2 Answers
Trouble detecting button release with Input.GetButtonUp() 1 Answer