SAT.AUG.15
2026
22:22:49

Driving the car

This page works through creating a full 3D racing game step-by-step

Adding Controls

To drive the car, you'll need to map these four inputs:

  1. "Forward" -> W key.
  2. "Backward" -> S key.
  3. "Left" -> A key.
  4. "Right" -> D key.

If you don't know how to do this, refer to this 3D game resource.

Adding a Script

You'll need to add a script onto your VehicleBody3D so you can customise and control your car.

extends VehicleBody3D

# Constants that control how the car behaves
var STEER_FORCE = 0.3 # Controls how strongly the car will steer left or right
var SPEED = 500       # Controls the maximum driving speed of the car

var max_torque = 500  # The force applied by the car's engine (acceleration)
var max_rpm = 2500    # Maximum revolutions per minute (RPM) for the wheels

var steer = 0.0  # Current steering angle (starts at 0, meaning no turn)

# Called when the node is added to the scene (for initialization)
func _ready() -> void:
	pass

# Called every frame, but we don't use it here since we're using physics
func _process(delta: float) -> void:
	pass

# Called at every physics frame (handles the car's movement and turning)
func _physics_process(delta: float):
	# Get the player input for turning left and right
	# 'Right' and 'Left' are inputs defined in the Input Map
	# This gives us a value between -1 (full left) and 1 (full right)
	var end_steer = Input.get_axis("Right", "Left") * STEER_FORCE
	
	# Smoothly adjust the steering angle based on how much time (delta) has passed
	# `lerp` smoothly moves `steer` toward `end_steer` at a speed of `steer_speed`
	var steer_speed = 5 * delta
	steer = lerp(steer, end_steer, steer_speed)
	
	# Apply the calculated steering value to the car's wheels
	set_steering(steer)

	# Get the player input for accelerating and decelerating
	# 'Backward' and 'Forward' are also inputs defined in the Input Map
	# This gives us a value between -1 (full reverse) and 1 (full forward)
	var acceleration = Input.get_axis("Backward", "Forward")

	# Control the force applied to the left rear wheel
	# The force decreases as the wheel reaches maximum RPM
	var rpm = abs($BackLeftWheel.get_rpm())  # Get the current RPM of the wheel
	$BackLeftWheel.engine_force = acceleration * max_torque * (1 - rpm / max_rpm)

	# Same logic as the left wheel
	rpm = abs($BackRightWheel.get_rpm())
	$BackRightWheel.engine_force = acceleration * max_torque * (1 - rpm / max_rpm)

Play the scene and drive the car!

If the speed and steer force still doesn't feel like a race car, you can change the VehicleBody3D's mass, or all the VehicleWheel3D wheels, suspensions, or damping properties.

If the Ground isn't big enough, set the scale to 2 (or however big you need it to be).

  • My car can drive like a race car

I know how to...

  • change my car to be slower
  • make the car take really sharp turns