- Home /
Other
Reverse Button problem
Hello!
I have a problem with my reverse button. It works if I use it at the start before pressing gas, but if gas is pressed even once then it doesn't work. Can someone spot my problem?
void FixedUpdate ()
{
currentSpeed = 2 * 22 / 7 * wheelBL.radius * wheelBL.rpm * 60 / 100;
currentSpeed = Mathf.Round (currentSpeed);
if (currentSpeed <= 0) {
reverseButton.GetComponent<CanvasGroup> ().alpha = 1;
reverseButton.GetComponent<CanvasGroup> ().interactable = true;
reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = true;
} else {
reverseButton.GetComponent<CanvasGroup> ().alpha = 0;
reverseButton.GetComponent<CanvasGroup> ().interactable = false;
reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = false;
}
if (gasOn) {
started = true;
Gas ();
}
if (reverseOn) {
Reverse ();
}
}
public void Gas ()
{
motorInputTouch = 1;
gasOn = true;
wheelBR.brakeTorque = 0;
wheelBL.brakeTorque = 0;
// First gear
if (currentSpeed <= 150) {
wheelBR.motorTorque = OneTorque * motorInputTouch;
wheelBL.motorTorque = OneTorque * motorInputTouch;
}
// Second gear
else if (currentSpeed <= 300 && currentSpeed > 150) {
wheelBR.motorTorque = TwoTorque * motorInputTouch;
wheelBL.motorTorque = TwoTorque * motorInputTouch;
}
// Third gear
else if (currentSpeed <= 450 && currentSpeed > 300) {
wheelBR.motorTorque = ThreeTorque * motorInputTouch;
wheelBL.motorTorque = ThreeTorque * motorInputTouch;
}
// Fourth gear
else if (currentSpeed > 450 && currentSpeed <= topSpeed) {
wheelBR.motorTorque = FourTorque * motorInputTouch;
wheelBL.motorTorque = FourTorque * motorInputTouch;
} else {
wheelBR.motorTorque = 0;
wheelBL.motorTorque = 0;
}
}
public void GasEnds ()
{
motorInputTouch = 0;
gasOn = false;
wheelBR.brakeTorque = maxBrakeTorque;
wheelBL.brakeTorque = maxBrakeTorque;
wheelBR.motorTorque = 0;
wheelBL.motorTorque = 0;
}
public void Reverse ()
{
motorInputTouch = 1;
reverseOn = true;
wheelFR.brakeTorque = 0;
wheelFL.brakeTorque = 0;
if (currentSpeed <= 0 && currentSpeed > -maxReverseSpeed) {
wheelBR.motorTorque = OneTorque * -motorInputTouch;
wheelBL.motorTorque = OneTorque * -motorInputTouch;
} else {
wheelBR.motorTorque = 0;
wheelBL.motorTorque = 0;
}
}
public void ReverseEnds ()
{
motorInputTouch = 0;
reverseOn = false;
wheelBR.brakeTorque = maxBrakeTorque;
wheelBL.brakeTorque = maxBrakeTorque;
wheelBR.motorTorque = 0;
wheelBL.motorTorque = 0;
}
At a quick glance, the only place I see reverseOn = true
is in the Reverse
function, which will never be reached should reverseOn = false
Are you setting reverseOn = true
somewhere else?
Reverse function is called when I press the reverse button! :)
I would check to make sure that currentSpeed
is actually 0 or less when you are pushing the reverse button. It would make sense that may be the issue because I presume the car is stopped when you first start the game. If you hit the gas, is there anywhere after that where you know the currentSpeed
would explicitly get set back to 0 or are you assu$$anonymous$$g it is by what you see?
It is 0. Just checked from the inspector while running the game.
I guess I would throw in some Debug.Log
lines. One at the top of the Reverse
function to make sure it's actually getting called, one after the if statement and one after you set the motorTorque
. $$anonymous$$ake sure A) that it is getting to all of those lines and B) that the motorTorque properties are what you would expect them to be after they are set.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Car game Reverse Button 1 Answer
Button's sprite swap works fine but it doesn't change the related image's source image! why? 1 Answer
Key for GUI.Button 2 Answers