Jumping Animation not working

Godot Version

4.6

Question

Can yall help me get my jump animation working

extends Node2D

@export var player : Player
@export var animation : AnimationPlayer
@export var Sprite : Sprite2D

func _process(_delta):
	if player.direction == 1:
		Sprite.flip_h = false
	elif player.direction == -1:
		Sprite.flip_h = true

	if abs(player.velocity.x) > 0.0 and not Input.is_action_pressed("Sprint"):
		animation.play("Walk")
	elif abs(player.velocity.x) > 0.0 and Input.is_action_pressed("Sprint"):
		animation.play("Sprint")
	elif abs(player.velocity.y) > 0.0 and Input.is_action_pressed("ui_accept") and player.is_on_floor():
		animation.play("Jump")
	else:
		animation.play("Idle")

For debugging, I’d add a “print(“We’re now playing the animation Jump”) after the jump animation line.

My guess is that the walk and sprint animations are overriding the jump animation, since any X velocity at all would trigger them to play.

it doesn’t even print and i updated the code a bit and the conditionals

extends Node2D

@export var player : Player
@export var animation : AnimationPlayer
@export var Sprite : Sprite2D

func _process(_delta):
	if player.direction == 1:
		Sprite.flip_h = false
	elif player.direction == -1:
		Sprite.flip_h = true

	if abs(player.velocity.x) > 0.0 and not Input.is_action_pressed("Sprint") and not Input.is_action_pressed("Jump"):
		animation.play("Walk")
	elif abs(player.velocity.x) > 0.0 and Input.is_action_pressed("Sprint") and not Input.is_action_pressed("Jump"):
		animation.play("Sprint")
	elif abs(player.velocity.y) < 0.0 and Input.is_action_pressed("Jump") and player.is_on_floor():
		animation.play("Jump")
		print("PLaying jump")
	else:
		animation.play("Idle")

here’s the player script just in case:

extends CharacterBody2D

class_name Player

#export
@export var acceleration := 2400.0
@export var speed := 100.0
const JUMP_VELOCITY = -300.0

# Variables
var direction = 0


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
	
	if Input.is_action_pressed("Sprint"):
		speed = 199.0
	else:
		speed = 100

	if Input.is_action_just_pressed("Jump") 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.
	direction = Input.get_axis("Left", "Right")
	velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)

	move_and_slide()

Try making your if elif chain check the conditionals for the jump animation first. I think what’s happening is it’s seeing x velocity first which makes it play the walk or sprint animation, and completely skip checking for the jump animation.

1 Like

it still doesn’t work i ran bout 5 tests

i fixed it