Menu
CoddyTech

Drive

Lesson 1 of 20 in Coddy's Godot Game: Neon Circuit course.

In this course you build one whole game, a feature a lesson: a neon racing game seen from above. You race three rival cars for three laps round a glowing track.

The project has three scripts for you: car.gd on the car, rival.gd for the rivals and race.gd for the race. Two more parts are given and stay out of sight: the track, which draws the road and answers questions about it, and the scene. You will not need to change them.

Today the car learns to drive. It keeps a velocity: how many pixels it moves each second, and which way. The way the car points is its rotation turned into a direction:

var forward := Vector2.RIGHT.rotated(rotation)

While Up is held, the velocity grows that way by accel a second. Input.get_action_strength is 1.0 while the key is down and 0.0 when it is not:

var throttle := Input.get_action_strength("move_up")
velocity += forward * throttle * accel * delta
velocity = velocity.limit_length(max_speed)
position += velocity * delta

limit_length keeps the speed at max_speed or under. Multiplying by delta, the seconds since the last frame, keeps the speed the same on a fast screen and a slow one.

challenge icon

Challenge

Easy
  1. In car.gd, replace pass in _physics_process with the lines above.
  2. Last, make the exhaust glow while you drive: $Exhaust.emitting = throttle > 0.0.
  3. Press Play and hold Up (or W).

Try it yourself

extends Node2D

@export var accel := 260.0
@export var max_speed := 200.0

var velocity := Vector2.ZERO


func _physics_process(delta: float) -> void:
	pass  # drive here

All lessons in Godot Game: Neon Circuit

Practice on your own: Godot playground