extends StaticBody2D # constants ## acceleration of the car while holding the accelerate button @export var acc := 20. ## natural deceleration of the car @export var dec := 4. ## maximum speed of the car @export var max_speed := 100. ## how many radians the car turns per second, higher value -> faster turns @export var turn_speed := 1. ## affects how fast the brakes, higher value -> quicker to stand still @export var brake_strength := 30. ## regulates how much more you can turn while breaking, values below 1 mean you can turn worse while drifting @export var drift_factor := 1.5 ## regulates how far the camera looks ahead of the player (affected by speed) @export var camera_offset_factor = 20. # references @export var camera : Camera2D # variables var momentum: Vector2 # Called when the node enters the scene tree for the first time. func _ready(): pass # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): var acc_d = acc * delta var dec_d = dec * delta var turn_d = turn_speed * delta var brake_d = brake_strength * delta var turn_factor = 1 if momentum.length() > dec_d: momentum -= momentum.normalized()*dec_d else: momentum = Vector2(0,0) if Input.is_action_pressed("accelerate"): momentum += acc_d * Vector2(1,0).rotated(rotation) if Input.is_action_pressed("brake"): turn_factor = drift_factor if momentum.length() > brake_d: momentum -= momentum.normalized() * brake_d else: momentum = Vector2(0,0) if Input.is_action_pressed("left"): rotate(turn_d*turn_factor*-1) if Input.is_action_pressed("right"): rotate(turn_d*turn_factor) camera.position = Vector2(momentum.length() * camera_offset_factor, 0) camera.rotation_degrees = momentum.angle() + 90 if momentum.length() > max_speed: momentum = momentum.normalized() * max_speed move_and_collide(momentum)