Please note that the “accelerate” input corresponds to moving forward, and the “brake” input corresponds to braking and moving backward.
In my case:
when “accelerate” input is released the brakes work (same for “brake” input is released)
when “accelerate” input is pressed and “brake” input too in the same time the brakes work (because the two cancel each other out)
But when “accelerate” input just released and “brake” input pressed just after that the brakes doesn’t work… I suppose that because my var moving_dir still !=0 and it never goes through zero.
How can I fix it please?
Here the script:
extends VehicleBody3D
@export var max_RPM = 1000
@export var max_torque = 2000
@export var turn_speed = 2
@export var turn_amount = 0.4
func _physics_process(delta: float) -> void:
var forward = Input.get_action_strength("accelerate")
var backward = - Input.get_action_strength("brake")
var moving_dir = forward + backward
var turn_left = Input.get_action_strength("left")
var turn_right = - Input.get_action_strength("right")
var steering_dir = turn_left + turn_right
var RPM_left = abs($BackL.get_rpm())
var RPM_right = abs($BackR.get_rpm())
var RPM = (RPM_left + RPM_right) / 2.0
var torque = moving_dir * max_torque * (1.0 - RPM / max_RPM)
engine_force = torque
steering = lerp(steering, steering_dir * turn_amount, turn_speed * delta)
if moving_dir == 0 :
brake = 50
I’m having trouble understanding how to add and check my vehicle’s inertia. Right now, in my code, I only have a positive or negative value for the direction, but nothing regarding the inertia of the VehicleBody3D node. So I think that if I can determine when the inertia is positive and apply a negative direction to it, the vehicle will first brake, then move in the negative direction (backward).