Can anyone help me with adding a boost to my car game?

Godot Version

My version is 4.7.2

Question

Im new to programming in godot so I know its definitely not super neat or optimized, but for me I think its pretty good!
I have really struggled adding a boost mechanic to my game that actually works. Basically I want it when you touch the shift button, you increase in speed over about a second and then slow down after than. Its then on hold for something like 5 seconds.

I just need help with this tiny bit, thank uu :face_holding_back_tears: :face_holding_back_tears:

(Also I have tried creating a timer but it either just pauses the car or just doesnt work)

extends CharacterBody2D

var rotation_speed = 3
var max_speedOG = 500
var max_speed = 500
var min_speedOG = -100
var min_speed = 0
var accel = 500
var deaccel = 4
var speed = 0

var ReverseIsFalse = 1
func _physics_process(delta: float) -> void:
	print(speed, "  speed  ", velocity, "  Velocity  ", ReverseIsFalse, "  ReverseIsFalse?  ")
	 #Shouldnt be if Vector2.zero as is means it will only slow down when you arent touching it (including turning)
	if speed >= max_speed: 
		speed = max_speed
	
	if not Input.is_action_pressed("Accelerate") or not Input.is_action_pressed("Brake"):
		if speed >= min_speed:
			speed -= sqrt(abs(speed))*delta
		else:
			speed += sqrt(abs(speed))*delta

	velocity = Vector2(1,min_speed).rotated(rotation)*speed*delta * speed/deaccel * ReverseIsFalse

	_movement(delta)
	move_and_slide()
func _movement(delta):
	var Dir = Input.get_vector("TurnRight","TurnLeft","Brake","Accelerate")
	if Input.is_action_pressed("TurnRight",true) or Input.is_action_pressed("TurnLeft",true): #Only turns if turning is true
		if Dir.x > 0:
			self.rotation -= rotation_speed*delta
		else:
			self.rotation += rotation_speed*delta
	if Dir.y < 0:
		if speed <= min_speed:
			if speed <= min_speedOG:
				ReverseIsFalse = -1
			else:
				ReverseIsFalse = -1
				speed -= (deaccel**deaccel*deaccel*delta)
		else:
			ReverseIsFalse = 1
			speed -= (accel*delta)
	elif Dir.y > 0:
		ReverseIsFalse = 1
		speed += accel*delta
	#if Input.is_action_just_pressed("Boost"): #If I get around to it.
		#max_speed*=2
		#speed*=2
	#else:
		#while max_speed > max_speedOG:
			#max_speed -= delta
			#print(max_speed)

What goes wrong with your presented implementation?


Sticking to basics you can accelerate with the move_toward function

var accel_input := Input.get_axis("Accelerate", "Brake")
# maybe adding (accel_input+1)/2 will help to keep the car rolling without any input pressed
velocity = velocity.move_toward(speed * accel_input, accel * delta)

Ive put it in as
var accel_input := Input.get_axis(“Brake”, “Accelerate”)
velocity = velocity.move_toward(speed * accel_input, accel * delta)
Just to see if it works however it doesn’t just returns this error
Invalid type in function ‘move_toward’ in base ‘Vector2’. Cannot convert argument 1 from float to Vector2. I know its something to do with the converting arg 1 (accel_input) to velocity however I don’t know how to change accel_input to a vector2)

Right, It should be similar to your other Vector2 rotation

var target_speed := Vector2(speed * accel_input, 0.0).rotated(rotation)
velocity = velocity.move_toward(target_speed, accel * delta)

I tried it and it worked perfectly! However it feels a lot more slippery which is not what I’m really going for within my game. It is actually crazy how you turned my code from like 50 lines down to 3. Thank you so much :face_holding_back_tears: :face_holding_back_tears: