Hey there, I’m trying to follow a 2D game making guide by freeCodeCamp.org on Youtube. In it they teach using the Animation Player and AnimatedSprite2D nodes to get the player animations working. I got most of the other things right but when I want to make the player swith to the Fall animation when falling, I can’t get to make it work. I’d be really happy if anybody could help me out. Thanks in advance.
Here’s the code:
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”) @onready var anim = get_node(“AnimationPlayer”)
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
anim.play("Jump")
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
if velocity.y == 0:
anim.play("Run")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.y == 0:
anim.play("Idle")
if velocity.y > 0:
anim.play("Fall")
move_and_slide()
this one can be complicated, how do you define it’s falling? because jump also not on the floor and will also fall to the floor once the momentum up is gone
As far as I understand, it needs to switch to the fall animation when the y velocity is greater than 0, so when it’s moving down. It needs to play the jump animation when the velocity is less than 0… I think.
Might have something to do with the animation player and the length of the jump animation if the jump animation looks a little off. Do you get any warnings (yellow errors) when trying to switch between animations?
i had a similar issue and that had something to do with the animationplayer
I just checked the animation player and I think I either forgot to key or deleted the fall animation frames. Thanks for the help, I’m sorry for the false alarm, I’m extremely new to these stuff