Dodge roll animation

Godot Version

4.5.1 stable

Question

So! I have been working on my first ever “tutorial” style game. Yknow the one, follow a few tutorials, use free assets and build a very bare bones game.
Now I am very new to Godot and couldn’t find a good tutorial on implementing a dodge roll.

Currently when I press dodge, the animation overrides everything else and freezes the player in place. Would appreciate a detailed fix and an explenation on why my dodge roll failed.
I know it has something to do with the “return” of the dodge roll, being places before move_and_slide.

Full code:
extends CharacterBody2D

const SPEED = 130.0
const JUMP_VELOCITY = -300.0

#Sets the idle state as a variable to allow dodge-roll override
var state = “idle”

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

#Makes sure not to override the roll animation with anythin
if state == "roll":
	return

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY

#gets input direction
var direction := Input.get_axis("move_left", "move_right")


#Flip the sprite
if direction > 0:
	animated_sprite.flip_h = false
if direction < 0:
	animated_sprite.flip_h = true
	
	
	#Dodge roll override animation
if Input.is_action_just_pressed("dodge") and is_on_floor():
	state = "roll"
	animated_sprite.play("Roll")
	await animated_sprite.animation_finished
	state = "idle" #resets player animation state
	return

	#Player movement animation
if is_on_floor():
	if direction == 0:
		animated_sprite.play("Idle")
	else:
		animated_sprite.play("run")
else:
	animated_sprite.play("jump")

	
#Apply movement
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()

Edit: Thanks for the replies, I followed some tutorials and still didn’t figure it out since I’m using a sprite-sheet animation and not a more complicated one. Other tutorials assumed you followed the whole series before you get to the dodge roll, so you end up calling on fuctions and actions you didn’t code. tis what it is, appreciate the help!

Try reading through this tutorial. I haven’t implemented it myself, but it seems like the direction you want to go.

1 Like

return is used to exit a function. This means, during the dodge roll animation, you exit _physics_process() before move_and_slide() is called, thus the player doesn’t move.

Adding an additional move_and_slide() before return should fix it:

	if state == "roll":
		move_and_slide()
		return
1 Like