Player State Machine #5

Open
opened 2026-05-04 21:07:41 +00:00 by OddlyTimbot · 1 comment
Owner

Add a finite state machine to track the player states and trigger animations.

Create an enumerator that will list all the possible states.

enum State{IDLE,JUMP,FALLING,RUNNING}
var current_state:State = State.IDLE

Make a variable to know when the player is jumping up, or has reached the apex of a jump and is falling.

jumpUp:bool = false

Add two new functions:

handle_state and handle_animation

Update the physics process to include these functions in the player loop.

func _physics_process(delta):
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	if current_state != State.HURT:
		handle_input()
	handle_movement(delta)
	update_states()
	update_animation()
	move_and_slide()
	handle_collisions()

In the handle_input function, detect a jump request and set the jumpUp variable to true:

if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		current_player_state = PlayerState.JUMPING
		jumpUp = true

Code for handling State:

func handle_state()->void:
	match current_player_state:
		PlayerState.IDLE when velocity.x !=0:
			#change to running!!
			current_player_state = PlayerState.RUNNING
		PlayerState.RUNNING when velocity.x == 0:
			current_player_state = PlayerState.IDLE
		PlayerState.JUMPING when velocity.y > 0:
			current_player_state = PlayerState.FALLING
		PlayerState.FALLING when is_on_floor():
			if velocity.x ==0:
				current_player_state = PlayerState.IDLE
			else:
				current_player_state = PlayerState.RUNNING

Code to handle animation:

func handle_animation()->void:
	match current_player_state:
		PlayerState.IDLE:
			player_graphic.play("idle")	
		PlayerState.RUNNING:
			player_graphic.play("running")
		PlayerState.JUMPING:
			if jumpUp:
				player_graphic.play("jumping")
		PlayerState.FALLING:
			player_graphic.play("falling")

We need to know when the jump animation has finished so we can set jumpUp to false:

func _on_animation_finished() -> void:
	match current_player_state:
		PlayerState.JUMPING:
			jumpUp = false
Add a finite state machine to track the player states and trigger animations. Create an enumerator that will list all the possible states. ``` enum State{IDLE,JUMP,FALLING,RUNNING} var current_state:State = State.IDLE ``` Make a variable to know when the player is jumping up, or has reached the apex of a jump and is falling. `jumpUp:bool = false` Add two new functions: `handle_state` and `handle_animation` Update the physics process to include these functions in the player loop. ``` func _physics_process(delta): # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. if current_state != State.HURT: handle_input() handle_movement(delta) update_states() update_animation() move_and_slide() handle_collisions() ``` In the handle_input function, detect a jump request and set the jumpUp variable to true: ``` if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY current_player_state = PlayerState.JUMPING jumpUp = true ``` Code for handling State: ``` func handle_state()->void: match current_player_state: PlayerState.IDLE when velocity.x !=0: #change to running!! current_player_state = PlayerState.RUNNING PlayerState.RUNNING when velocity.x == 0: current_player_state = PlayerState.IDLE PlayerState.JUMPING when velocity.y > 0: current_player_state = PlayerState.FALLING PlayerState.FALLING when is_on_floor(): if velocity.x ==0: current_player_state = PlayerState.IDLE else: current_player_state = PlayerState.RUNNING ``` Code to handle animation: ``` func handle_animation()->void: match current_player_state: PlayerState.IDLE: player_graphic.play("idle") PlayerState.RUNNING: player_graphic.play("running") PlayerState.JUMPING: if jumpUp: player_graphic.play("jumping") PlayerState.FALLING: player_graphic.play("falling") ``` We need to know when the jump animation has finished so we can set jumpUp to false: ``` func _on_animation_finished() -> void: match current_player_state: PlayerState.JUMPING: jumpUp = false ```
OddlyTimbot added the
enhancement
label 2026-05-04 21:35:50 +00:00
Author
Owner

Handle the jump and fall states.
The trick with the jump state is to trigger the animation only at the start of the jump, then hold the last frame as long as the player is moving upwards.
When the player is overcome by gravity and begins to fall, detect that and trigger the fall animation.

During falling, check if the player reaches the ground, then go to either IDLE or RUN states.

Handle the jump and fall states. The trick with the jump state is to trigger the animation only at the start of the jump, then hold the last frame as long as the player is moving upwards. When the player is overcome by gravity and begins to fall, detect that and trigger the fall animation. During falling, check if the player reaches the ground, then go to either IDLE or RUN states.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: OddlyTimbot/AprilGameExample#5
No description provided.