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
```
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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Add a finite state machine to track the player states and trigger animations.
Create an enumerator that will list all the possible states.
Make a variable to know when the player is jumping up, or has reached the apex of a jump and is falling.
jumpUp:bool = falseAdd two new functions:
handle_stateandhandle_animationUpdate the physics process to include these functions in the player loop.
In the handle_input function, detect a jump request and set the jumpUp variable to true:
Code for handling State:
Code to handle animation:
We need to know when the jump animation has finished so we can set jumpUp to false:
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.