Godot Version
4.3
Question
I’m currently trying to implement a way to make the player accelerate and decelerate slower when on an icy surface. Currently, the code looks like this:
var input_dir = Input.get_vector("A", "D", "W", "S")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if(direction and running):
if(iced):
velocity.x = move_toward(velocity.x, direction.x * RUN_SPEED, icyFriction)
velocity.z = move_toward(velocity.z, direction.z * RUN_SPEED, icyFriction)
elif(!is_on_floor()):
velocity.x = move_toward(velocity.x, direction.x * RUN_SPEED, AIR_FRIC)
velocity.z = move_toward(velocity.z, direction.z * RUN_SPEED, AIR_FRIC)
else:
velocity.x = direction.x * RUN_SPEED
velocity.z = direction.z * RUN_SPEED
elif(direction and !running):
if(iced):
velocity.x = move_toward(velocity.x, direction.x, icyFriction)
velocity.z = move_toward(velocity.z, direction.z, icyFriction)
elif(!is_on_floor()):
velocity.x = move_toward(velocity.x, direction.x * SPEED, AIR_FRIC)
velocity.z = move_toward(velocity.z, direction.z * SPEED, AIR_FRIC)
else:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
elif(iced):
velocity.x = move_toward(velocity.x, 0, icyFriction)
velocity.z = move_toward(velocity.z, 0, icyFriction)
elif(!is_on_floor()):
velocity.x = move_toward(velocity.x, 0, AIR_FRIC)
velocity.z = move_toward(velocity.z, 0, AIR_FRIC)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
running = false
However, when I release a button while facing between any 90 or 45 degree angle, it causes me to go sideways at a fairly consistent angle, being more noticeable at higher velocities.
It is not caused by the deceleration itself, but instead it is something to do with when the acceleration stops. Is there a better method of implementing acceleration?