Car stops breaking when I press acceleration

I’ve downloaded a neat (free) vehicle script that I’m modifying a bit to better suit my needs. For instance, the wheels didn’t stop turning when the handbrake was applied, which is now fixed. Also, acceleration and braking where tied to one axis, which I separated. One thing that I can’t get to work is the following:

When I’m accelerating (press up) and brake (press down) at the same time, the car won’t brake. When I only brake (when moving) the wheels stop turning and the car brakes. As soon as I press accelerate again the brakes are released even when I’m still braking.

Up pressed = accelerating
Down pressed = braking (and reversing if the speed reaches 0)
Up & down pressed = accelerating

This is the piece of code that handles the part that I’m struggling with.

        float speed = GetSpeed();
        isAcceleration = false;
        isReverseAcceleration = false;
        if (v > 0.4f)
        {
            if (speed < -0.5f)
            {
                isBrakeNow = true;
            }
            else
            {
                isAcceleration = true;
            }
        }
        else if (b > 0.4f)
        {
            if (speed > 0.5f)
            {
                isBrakeNow = true;
            }
            else
            {
                isReverseAcceleration = true;
                isAcceleration = false;
            }
        }

For some reason the acceleration part keeps overruling the braking part. I’ve tried searching for a solution but didn’t really find one. Anyone here that can point me in the right direction?

Second less important question: is it possible to not reverse immediately when reaching 0 speed? So first brake to 0 and to reverse you have to release ‘down’ and press it again.

It is the else statement on line15.

Follow the logic: if you are accelerating it does not even consider braking.

I think you can remove that else and it might just work… at least it’s something to try.

There are some checks on the input (comparing it to 0.4 and -0.4) and checks on the speed (comparing it to 0.5 and -0.5)… identify how those affect the behavior you want to get.

For realtime interaction and instrumentation, it can be handy to sprinkle in lots of Debug.Log() statements so you can see the realtime values of variables involved in the decisions this program makes.

I’m going to second removing the Else If statement. But if that doesn’t fix it, then the problem might not be in that particular section of code. Can we see how you’re getting your inputs and what you’re doing with the outputs of this part of the code?

Removing the Else if doesn’t work as it completely stops me from braking. The speed variable is used to gently apply brakes instead of instantly stopping the car.

        float v = Input.GetAxis("Gas");
        float b = Input.GetAxis("Brake");

That’s used as the input.

Edit: I think I misunderstood. I’ve removed ‘Else’ and now it works as intended! Thanks :slight_smile:

Next up for me is figuring out how to first get to 0 when braking and only reversing when releasing and pressing braking again.

Sweet!

I think you only need one extra boolean variable. Let’s call it “going forward”

Whenever you hit the gas, going forward becomes true.

Whenever your speed gets very near zero (don’t check for equality to zero but rather say, “less than 0.1f or so”), clear that “going forward” variable, but ONLY do that check when the brake button is NOT pressed.

This means if you are going forward under power, the “going forward” bool stays true, even if you let go of the gas or hit the brake.

only when you hit the brake and bring your speed close to zero, then RELEASE the brake, does the clear happen.

And obviously, braking will REVERSE you when that boolean is false, otherwise it only slows you to zero.

1 Like

So basically something like:

If going forward and braking

  • reverse = false

If speed is less than 0.1 and the brake is released

  • reverse = true

If the speed is less than 0.1, reverse = true and the brake is applied again apply a negative speed to actually reverse.

I can work with that :slight_smile: But that’s something for tomorrow as it’s already quite late. Thanks again for the help!

Of course! It sounds like you summarized it fine, with an inverted sense of the boolean, saying “reverse” instead of “going forward,” and either one is probably fine if it makes in-brain sense to you.

That’s the thing with coding for me. I know the logic behind it but converting the theory to practice isn’t my strong point. But you gotta start somewhere if you want to turn your static art into interactive art :slight_smile:

1 Like