Car Velocity like real car

Godot Version

4.2.1

Question

Hello Everyone, I’m new to Godot and i still learning it. right now im following the car steering tutorial from KidsCanCode, i finish the tutorial but i feel the car velocity not realistic. the car speed just jump from 0 to max, i want it to have progress like real gear or at least there progression from zero to slow to full speed. So my question is how can i modified the code so the car at least have speed progression or there other tutorial for racing game i can follow ? thanks in advance and sorry for my bad english.

here the link to the tutorial :
Car_Steering

and here my car code :

extends CharacterBody2D

class_name Player

var wheel_base = 70
var steering_angle = 12
var engine_power = 800
var friction = -55
var drag = -0.06
var braking = -450
var max_speed_reverse = 250
var slip_speed = 400  
var traction_fast = 2.5 
var traction_slow = 10  

var acceleration = Vector2.ZERO
var steer_direction

func _physics_process(delta):
	acceleration = Vector2.ZERO
	get_input()
	apply_friction(delta)
	calculate_steering(delta)
	velocity += acceleration * delta
	move_and_slide()

func get_input():
	var turn = Input.get_axis("steer_left","steer_right")
	steer_direction = turn * deg_to_rad(steering_angle)
	if (Input.is_action_pressed("accelerate")):
		velocity = transform.x * engine_power
	if (Input.is_action_pressed("brake")):
		acceleration = transform.x * braking

func calculate_steering(delta):
	var rear_wheel = position - transform.x * wheel_base/2.0
	var front_wheel = position + transform.x * wheel_base/2.0
	rear_wheel += velocity * delta
	front_wheel += velocity.rotated(steer_direction) * delta
	var new_heading = rear_wheel.direction_to(front_wheel)
	var traction = traction_slow
	if velocity.length() > slip_speed:
		traction = traction_fast
	var d = new_heading.dot(velocity.normalized())
	if d > 0:
		velocity = lerp(velocity, new_heading * velocity.length(), traction * delta)
	if d < 0:
		velocity = -new_heading * min(velocity.length(),max_speed_reverse)
	rotation = new_heading.angle()

func apply_friction(delta):
	if acceleration == Vector2.ZERO and velocity.length() < 50:
		velocity = Vector2.ZERO
	var friction_force = velocity * friction * delta
	var drag_force = velocity * velocity.length() * drag * delta
	acceleration += drag_force + friction_force

1 Like

After running your code in a new project and experiencing the same thing, I started looking at the code.
It appears you have not followed the tutorial properly, or the tutorial made a mistake.

Instead of

if (Input.is_action_pressed("accelerate")):
		velocity = transform.x * engine_power

it should be

if (Input.is_action_pressed("accelerate")):
		acceleration = transform.x * engine_power
1 Like

oooo, my bad probably I’m probably mess with the script or my eyes not focus, thanks btw sir. I Appreciate your answer~

1 Like