Skip to content

Commit bded14a

Browse files
authored
Automatic brake/reverse
Value fwd_mps converts global velocity into a velocity vector which is rotated to cars local coordinate system (in this example x-axis). Then if key "ui_down" is pressed and the fwd_mps value is greater or equal to -1 (which seems to be very good spot and represents car still moving forward but nearly standing still) the car reverses (negative engine_force_value). If this condition isn't met (which means the car is moving forward) then the car brakes. Tested and working on Godot 3.1 Beta 3. Suggestions: In this case, if you want something more realistic, the -engine_force_value can be much lower because no car reverses in same speed as it goes forward but for the sake of demo project I left it as it is. Created for my own project with help of user wombatstampede from godotdevelopers.org/forum
1 parent a6ce6db commit bded14a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

3d/truck_town/vehicle.gd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var steer_target = 0
1010
export var engine_force_value = 40
1111

1212
func _physics_process(delta):
13+
var fwd_mps = transform.basis.xform_inv(linear_velocity).x
14+
1315
if Input.is_action_pressed("ui_left"):
1416
steer_target = STEER_LIMIT
1517
elif Input.is_action_pressed("ui_right"):
@@ -23,7 +25,10 @@ func _physics_process(delta):
2325
engine_force = 0
2426

2527
if Input.is_action_pressed("ui_down"):
26-
brake = 1
28+
if (fwd_mps >= -1):
29+
engine_force = -engine_force_value
30+
else:
31+
brake = 1
2732
else:
2833
brake = 0.0
2934

0 commit comments

Comments
 (0)