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
![]()
(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)