Search Unity

Car stops breaking when I press acceleration

Discussion in 'Scripting' started by SomerenV, Aug 12, 2020.

  1. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    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.
    Code (CSharp):
    1.         float speed = GetSpeed();
    2.         isAcceleration = false;
    3.         isReverseAcceleration = false;
    4.         if (v > 0.4f)
    5.         {
    6.             if (speed < -0.5f)
    7.             {
    8.                 isBrakeNow = true;
    9.             }
    10.             else
    11.             {
    12.                 isAcceleration = true;
    13.             }
    14.         }
    15.         else if (b > 0.4f)
    16.         {
    17.             if (speed > 0.5f)
    18.             {
    19.                 isBrakeNow = true;
    20.             }
    21.             else
    22.             {
    23.                 isReverseAcceleration = true;
    24.                 isAcceleration = false;
    25.             }
    26.         }
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
  4. VestedGamr

    VestedGamr

    Joined:
    Dec 19, 2013
    Posts:
    12
    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?
     
  5. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    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.

    Code (CSharp):
    1.         float v = Input.GetAxis("Gas");
    2.         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 :)

    Next up for me is figuring out how to first get to 0 when braking and only reversing when releasing and pressing braking again.
     
    Last edited: Aug 12, 2020
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
    omnivore likes this.
  7. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    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 :) But that's something for tomorrow as it's already quite late. Thanks again for the help!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
  9. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    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 :)
     
    Kurt-Dekker likes this.