Godot Version
4.5.1
Question
Hi, first of all I’m a rookie regarding development.
I’m trying to create a platformer 2D with momentum and i have a running state in physics process.
My goal is to make the player change direction with momentum at a precise timing else he’ll go back to a walk state.
I’m having issues on how to define the timing and take the input at said time. The thing is i get to change direction but not in a consistant way at all and it need the action to be spammed like hell. I’m trying to have the player push the button one time at te right timing to reward him with momentum.
I’m not sure that i’m been clear feel free to ask any question !
Bellow you’ll find all the code related to my Run state :
func _physics_process(delta: float) -> void:
var x_input = Input.get_axis("move_left", "move_rigth")
apply_gravity(delta)
states.RUN:
# if player ask to change direction launch timer
if change_dir and accel_timer.is_stopped():
accel_timer.start()
if is_on_floor():
# if x_input = direction and not changing direction than move forward
if x_input == x_dir and change_dir == false:
accelerate_horizontaly(x_dir, delta)
anchor.scale.x = sign(x_dir)
# if player want to change direction => check if input is on purpose
if x_input != x_dir and not change_dir:
change_dir = true
run_change_dir_timer.start()
# if change of direction and no x_input => go on stop state
if x_input == 0 and can_change:
can_change = false
change_dir = false
animation_camera.play("SlowDown")
state = states.STOP
# if change of direction and button pushed whilde change direction timer on than direction = x_input
if x_input != 0 and x_dir != x_input and can_change:
change_dir = false
can_change = false
apply_friction(delta)
accel_timer.start()
if Input.is_action_just_pressed("action") and velocity.x == 0.0 or accel_timer.time_left > 0.0:
store_time = false
x_dir = x_input
#else :
#state = states.WALK
#animation_camera.play("SlowDown")
# if grounded and not changing direction than jump
if (is_on_floor() or coyote_jump_timer.time_left > 0.0) and not change_dir:
if Input.is_action_just_pressed("jump"):
velocity.y = -movement_data.jump_velocity
coyote_jump_timer.stop()
if not is_on_floor():
animation_player_lower.play("jump")
animation_player_upper.play("jump")
else:
animation_player_upper.play("run")
animation_player_lower.play("run")
move_and_slide()
func accelerate_horizontaly(horizontal_direction: float, delta: float) -> void:
var current_state_speed = movement_data.speed
var acceleration_amount: float = movement_data.acceleration
if not is_on_floor(): acceleration_amount = movement_data.air_acceleration
velocity.x = move_toward(velocity.x, current_state_speed * horizontal_direction, acceleration_amount * delta * abs(horizontal_direction))
func apply_friction(delta) -> void :
var friction_amount: float = movement_data.friction
if not is_on_floor(): friction_amount = movement_data.air_friction
velocity.x = move_toward(velocity.x, 0.0, friction_amount * delta)
func apply_gravity(delta) -> void:
if not is_on_floor():
if velocity.y <= 0:
velocity.y += movement_data.up_gravity * delta
else:
velocity.y += movement_data.down_gravity * delta
func _on_run_change_dir_timer_timeout() -> void:
# if input is maintained than bool becomes true and change direction allowed
can_change = true
run_change_dir_timer.stop()
func _on_accel_timer_timeout() -> void:
# When timer reach 0, if in Run state than velocity = 0
store_time = true
if state == states.RUN :
pass
#velocity.x = 0