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.
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.
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
Next up for me is figuring out how to first get to 0 when braking and only reversing when releasing and pressing braking again.
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.
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