Roll animation will not execute when CTRL is pressed

v 4.2.2
GODOT 4

I am taking the “Brackeys course How to Make a Video Game - Godot Beginner Tutorial.” I am trying to add the roll animation, on my own, since the tutorial never showed how. When I hit the CRTL key the player does not roll. All other animation work but the roll. Can anyone figure out what is happening?

extends CharacterBody2D

const SPEED = 100.0
const JUMP_VELOCITY = -300.0

Get the gravity from the Project Settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”) # default value of 980.
@onready var animated_sprite = $AnimatedSprite2D ## references AnimatedSprite2D Node

func _physics_process(delta)

if not is_on_floor():
	velocity.y += gravity * delta

# Handle jump.

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

	# Flips the sprite 
if direction > 0:
	animated_sprite.flip_h = false
elif direction < 0:
	animated_sprite.flip_h = true

# Play animations

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

**if Input.is_action_just_pressed("Ctl_Button") and is_on_floor():**

** animated_sprite.play(“roll”)**

# Apply movement

if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED) # effectively makes the velocity 0

move_and_slide()

Maybe the animation name is case-sensitive? Sorry not really sure.

The case is correct but thanks for the input.

is there any response if you try removing the ‘is_on_floor’ part for a moment?

while you’re at it, maybe throw a print(‘got roll input’) in that if…then so you can confirm the input key is correctly mapped and reference

Adding print (‘You rolled.’) does show printing to output when the key is pushed but removing the is_on_floor does not help.

1 Like

OK thanks for confirming that. So looking through the various if…else… etc branches in your sample, I see some things that could be an issue, but it’s hard to tell because the formatting got a little messed up in the forum post, maybe from where you added the **'s to highlight the line in question. So if you’re able to edit the post so it shows the exact, correctly tabbed (so we can figure out the logic branches) code you’re trying to fix, that’d be great. Or reply with a new post, with that code, but with no edits etc.

The main thing I’m trying to figure out is ok, after you check if is on floor, under the “# Play Animations” comment, basically no matter what the current conditions are, it seems like it’s going to play some animation, probably idle or run, unless in midair, then jump. So every time through that logic branch there, there will be an animation played. Which seems like a problem, because sometimes you want to play the roll, and in that case, the branch before it should not make any changes to the animation state.

So as another test, here’s what I’d try (until we can get a clear, unbugged view of the code):

before the ‘if is_on_floor()’ line, add a new ‘if’ line. That if…then will have a conditional block that contains the if is_on_floor() line and the next 8 lines after it (the ones that play animations or decide which animation to play, up to and including the lines that start the roll animation). So none of those lines run if the new if… then conditional does not pass the test. And for that new if line, maybe something like this:


if not (animated_sprite.is_playing() && animated_sprite.animation == "roll"):
    # now tab in one level to create a conditional block
    # ... and put those existing animation control logic lines inside that block
    if is_on_floor():
        if direction == 0:

(etc etc)

so what that new line should do is check if you’re currently in the middle of a roll animation, and if so, make no changes to the animation state until that animation is finished.

I think that might fix it up right there. But if you can update OP or post a new message with the code not-messed-up-by-formatting, we can be a little more sure of that.

@ MortalWombat It worked! and you are amazing. Thanks so much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.