How can i make my character change frames depending on velocity?

Godot Version

4.1.1

Question

I’ve been having trouble trying to make this jump animation work where the velocity of the player changes what the frame of the animation plays. I barely know enough about code to solve this, but what i am trying to have is have frames 0 through 5 play depend on the velocity of the player with 0 being the highest (like the first frame of jumping) and 5 being the lowest (falling really far down)

if is_on_floor():
if direction == 0:
animated_sprite.play(“idle”)
else:
animated_sprite.play(“run”)
else:
if velocity.y <= 100:
animated_sprite.play(“jump0”)
if velocity.y <= 40:
animated_sprite.play(“jump1”)
if velocity.y <= 0:
animated_sprite.play(“jump2”)
if velocity.y >= 0:
animated_sprite.play(“jump3”)
if velocity.y >= 40:
animated_sprite.play(“jump4”)
if velocity.y >= 100:
animated_sprite.play(“jump5”)

I started this using the recent brackeys tutorial, but after finding nothing on changing individual frames I started to make 6 different animations with each frame in it, but then that didn’t work and after trying any other method it would play only the frames 2 and 3. I am sorry for having barely any knowledge on stuff like this.

currently the bottom seems to work pretty well, but jump0 and jump1 do not play at all

change to elif

is there a specific that i should use for this because ive tried so many combinations of elif that all haven’t worked (like elif all after the first, the jump2 one having elif, elif on every other one)

if your jump0 to jump5 are actually animation name with sprites inside it, it should work
else you will need to stop the animation before playing any of the jump animation

the each jump if should be elif (other than the first if, of course)

so

else:
if velocity.y <= 200:
animated_sprite.play(“jump0”)
elif velocity.y <= 80:
animated_sprite.play(“jump1”)
elif velocity.y <= 0:
animated_sprite.play(“jump2”)
elif velocity.y >= 0:
animated_sprite.play(“jump3”)
elif velocity.y >= 80:
animated_sprite.play(“jump4”)
elif velocity.y >= 200:
animated_sprite.play(“jump5”)

I tried this and when i did, jump0 would play instead of jumps 1 and 2

okay i figured out that making the first two negative, actually works. Thank you!

yes, because jump is from 0 to minus first then to positive y value
so your first <=200 didnt work as expected because it’s always minus first and will perceived as jump 0

1 Like