• Edited

Am I doing something wrong? I've done acceleration correct before, but I lost that code. For some reason my character is moving very slowly instead of gradually moving toward a higher speed.

My code:

Variables

@export var acceleration = 5.0
@export var speed = 0 # default
@export var speed_move = 100 # speed when moving with input
@export var speed_max = 999 # maximum speed

Move State:

States.MOVE:
			if velocity.is_zero_approx() and ! Input.is_action_pressed("move"):
				state = States.IDLE # change state
			elif Input.is_action_pressed("space_bar"):
				state = States.CHARGE # change state
			else: # run move code
				velocity = direction_input * speed # move
				speed = speed_move # set speed to move speed
				move_and_slide()
				apply_acceleration()

acceleration function:

func apply_acceleration():
	speed = move_toward(speed, speed_max, acceleration)
  • You're re-initializing speed to the constant value speed_move before calling move_toward(). Doesn't that mean that move_toward() will always return the same value for speed? And then velocity will be computed from that same value, so move_and_slide()will always use that same value of velocity.

    Try printing the values of speed and velocity to verify that.

    Maybe the solution is to only do this once, when leaving the IDLE state:
    speed = speed_move

You're re-initializing speed to the constant value speed_move before calling move_toward(). Doesn't that mean that move_toward() will always return the same value for speed? And then velocity will be computed from that same value, so move_and_slide()will always use that same value of velocity.

Try printing the values of speed and velocity to verify that.

Maybe the solution is to only do this once, when leaving the IDLE state:
speed = speed_move

    DaveTheCoder Thank you! I'm so silly. Been sick and unable to think straight. Thank you so much for your help.

      NJL64 silly

      I've been coding a long time, but I still make silly mistakes which cost me days debugging. But debugging is my favorite part of coding, so there's an up-side to it.