You just need to use the relative forward direction of the KinematicBody2D to move forward and backwards. You can calculate the relative forward direction vector using something like this (you may need to flip the cos and sin):
var forward = Vector2(cos(rotation), sin(rotation))
And that will give you a normalized vector pointing in the direction the car is facing. In theory, the basic code for making a car like you are wanting should look like this (not taking acceleration or velocity into account, and this code is untested!):
extends KinematicBody2D
var move_speed = 20;
var rotation_speed = 4;
func _physics_process(delta):
#First, let’s handle left/right rotation of the car
if Input.is_action_pressed(“left”):
rotation += deg2rad(rotation_speed) * delta;
If Input.is_action_pressed(“right”):
rotation -= deg2rad(rotation_speed) * delta;
#Now let’s calculate the vector pointing forward from the car
var forward = Vector2(cos(rotation), sin(rotation))
#All that is left is moving the car in the right direction!
If Input.is_action_pressed(“up”):
move_and_slide(forward * move_speed * delta);
elif Input.is_action_pressed(“down”):
move_and_slide(-forward * move_speed * delta);
To make it work with velocity and acceleration, I’d reccomend looking at the official Godot demos, as I imagine there is a demo showing how code a system that uses velocity and acceleration in 2D. If you don’t mind translating the code from 3D to 2D, you could probably use the player code from the first part of the FPS tutorial (disclaimer: I wrote the tutorial)