Jump Animation is not playing

Hi folks. I’m new to Godot and was having some trouble getting an animation to play after putting it in code. I have done everything correctly from correct capitalisation and the coding is the same as the tuorial that I am following, yet it is not working. If someone could please look at the following code and tell me what the issue is, that would be most helpful, thanks.

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

@onready var anim = get_node(“AnimationPlayer”)

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

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

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
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")

move_and_slide()
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):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY
	anim.play("Jump")

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
	velocity.x = direction * SPEED
	anim.play("Run")
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	anim.play("Run")

#handle stop
if velocity.y == 0:
	anim.play("Idle")

move_and_slide()

What I am having trouble with specifically is that Jump animation is not playing. The error that comes in says “character_body_2d.gd:19 @ _physics_process(): Animation not found: Jump.” I’ve spelt everything correctly and all the other functions are working.

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):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY


# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

if not is_on_floor:
	anim.play("Jump")
if is_on_floor && velocity.y == 0:
	anim.play("Idle")
if is_on_floor && velocity.y != 0:
	anim.play("Run")

move_and_slide()

Thank doesn’t work, it just plays the walking animation, but thanks for helping anyway :slight_smile:

do you have an animated sprite 2D ???

Yeah I’m doing something else now. I looked at another forum and I came up with this " extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

@onready var anim = get_node(“AnimationPlayer”)

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
else:
var _Jump := false
if Input.is_action_just_pressed(“ui_accept”) and is_on_floor():
velocity.y = JUMP_VELOCITY
$AnimatedSprite2D.play(“Jump”)
_Jump = true

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
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") 
move_and_slide()

"

The problem now is that it only plays the jump animation briefly, and goes back to idle whilst it is in the air.

try this

Yeah that doesn’t work either. I don’t this problem is the code, its the fact Godot thinks that the animation player does not have Jump in it despite that it does.