Godot Version
4
Question
when i use this movement script i’ve made for myself it feels almost random how much speed i get when i dash, this is my first game that i am taking seriously so help would really be appreciated
the way it currently works is, when moving, the character can reach up to 606 speed, or it can just get 595 (negative if i’m moving to the left,) if switch my directions quickly and have at least 450 in either direction i’ll receive a speed bonus but that bonus is completely random and seems to be higher depending on how fast i switch what key i’m pressing, but that might just be me, the values i’ve recorded from switching speeds have been a minimum of 1005 with up to 1800 and but without even slowing down, so the character just stays in 1800 forever
even worse, when the “dash” is over, the character will sometimes have a greater speed than it’s intended or sometimes less
tldr; my code is awful and is making my feature seem more like a bug than anything, all i want it to do is for the character to have a speed boost when switching directions quickly
this is what the code looks like:
extends CharacterBody2D
const max_speed = 600
const accel = 700
const extra_accel = 14000
const time_to_stop = 1.3
const real_max_speed = 1800
func _physics_process(delta):
var input = Vector2.ZERO
input.x = Input.get_axis(“left”, “right”)
input.y = Input.get_axis(“up”, “down”)
var target_speed_x = (max_speed * input.x)
if input.x == 0 and velocity.x != 0:
velocity.x = (velocity.x / time_to_stop)
elif input.x != 0 and velocity.x > max_speed or velocity.x < (max_speed * -1):
if velocity.x > max_speed:
velocity.x -= (input.x * accel * delta)
if velocity.x < (max_speed * -1):
velocity.x -= (input.x * accel * delta)
else:
if input.x > 0 and velocity.x < 0:
if velocity.x <= -450:
velocity.x += ((accel * delta + real_max_speed) * input.x)
else:
velocity.x += (input.x * (accel + extra_accel) * delta)
elif input.x < 0 and velocity.x > 0:
if velocity.x >= 450:
velocity.x += ((accel * delta + real_max_speed) * input.x)
else:
velocity.x += (input.x * (accel + extra_accel) * delta)
else:
velocity.x += (input.x * accel * delta)
velocity.x = clamp(velocity.x, (real_max_speed * -1), real_max_speed)
print(velocity.x)
print(input.x)
move_and_slide()