Jumping anim is not playing

→ Godot Version
4.2.1

So when i try to jump with my char, let’s say the character is not moving and playing Idle anim, When i jump instead of playing the Jumping anim it continues playing the Idle anim, Or let’s say the char is moving and the walking anim is playing, then when i jump instead of jumping anim playing, the walking anim plays instead!

Info (not that important)

Runnin_VV is just the running speed.

CODE:

extends CharacterBody2D

const Runnin_VV = 1000
const SPEED = 500
const MOONWALK_SPEED = 750
const JUMP_VELOCITY = -650
const JUMP_MAX_HEIGHT = -650

@onready var sprite = $AnimatedSprite2D
@onready var rc = $RayCast2D

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

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

**if Input.get_action_strength("ui_accept") && is_on_floor():**

** velocity.y = JUMP_VELOCITY**
** sprite.play(“jump”)**
** is_jumping = true**


** if Input.get_action_strength(“high_jump”) && is_on_floor():**
** velocity.y = JUMP_MAX_HEIGHT**
** is_jumping = true**
** if is_jumping == true:**
** sprite.play(“jump”)**

var direction = Input.get_axis("ui_left", "ui_right")
var is_running_right = Input.is_action_pressed("Running_Right")
var is_running_left = Input.is_action_pressed("Running_Left")

if is_running_right:
	sprite.play("Running")
	velocity.x = Runnin_VV
	if direction < 0:
		sprite.flip_h = false
elif is_running_left:
	sprite.play("Running")
	velocity.x = -Runnin_VV
	if direction > 0:
		sprite.flip_h = true
elif direction != 0:
	velocity.x = direction * SPEED
	if sprite.animation != "Walk":
		sprite.play("Walk")
else:
	velocity.x = 0
	if sprite.animation != "idle":
		sprite.play("idle")

if Input.is_action_pressed("moon_walk"):
	if direction != 0 and rc.is_colliding():
		sprite.play_backwards("Walk")
		velocity.x = -MOONWALK_SPEED * direction
	else:
		velocity.x = -MOONWALK_SPEED * sign(velocity.x)

if Input.is_action_just_pressed("ui_left"):
	sprite.flip_h = true
elif Input.is_action_just_pressed("ui_right"):
	sprite.flip_h = false

if is_on_floor():
	is_jumping = false

move_and_slide()

(I appreciate any help!)

Hey there!

I’m pretty new to Godot itself, so take this with a pinch of salt, but it looks like your animations might not be properly handling the jumping state as jumping is separated from the rest of the movement animation logic. This could be why your character continues playing animations like walking or idle even when jumping.

I’d probably suggest trying to separate your animation logic from your movement logic. This way, you can check if the character is jumping first, and if so, only play the jump animation and skip the others until the character lands. This should help make sure the jump animation takes priority. Meanwhile, you can still handle left and right movement separately, so your character can move around in the air.

If that’s not quite clear or you need more details, just let me know and I’ll try help as best i can.

1 Like

So you think i should make a different script to handle anims?
so that way the movement and the anims doesn’t like mess each other up?

im new 2, and i dont got much code experience, Thank you for replying, I will try your suggestion!

after i try i will say if it worked :+1:

again thank you.

Doesn’t have to be a different script, just a separate if-else block, for example, one block that’s:

If jumping:
Play jump animation
Else if moving right
Play moving right animation
Etc.

If jumping
Do jumping movement

If moving right
Move player right
Else if moving left
Move player left
Etc.

Does what make more sense? Sorry I suck at explaining sometimes :sweat_smile:

1 Like

Ei man thank you for the help did what you asked separated the animation
logic from the movement, so like if your jumping without moving Idle wont play because jumping is already playing, and if moving on air, R & L Walk anim won’t play because the jumping anim is already playing. :smile:

CODE:

extends CharacterBody2D

const Runnin_VV = 1000
const SPEED = 500
const MOONWALK_SPEED = 750
const JUMP_VELOCITY = -560
const JUMP_MAX_HEIGHT = -760

@onready var sprite = $AnimatedSprite2D
@onready var rc = $RayCast2D

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var is_jumping = false

func _physics_process(delta):

	
	if not is_on_floor():
		velocity.y += gravity * delta
	
	
	if Input.get_action_strength("ui_accept") && is_on_floor():
		velocity.y = JUMP_VELOCITY
		is_jumping = true
	
	
	if Input.get_action_strength("high_jump") && is_on_floor():
		velocity.y = JUMP_MAX_HEIGHT
		is_jumping = true
	
	if not is_on_floor():
		if sprite.animation != "jump":
			sprite.play("jump")
	else:
		is_jumping = false
	
	var direction = Input.get_axis("ui_left", "ui_right")
	var is_running_right = Input.is_action_pressed("Running_Right")
	var is_running_left = Input.is_action_pressed("Running_Left")
	
	if is_running_right && is_on_floor():
		velocity.x = Runnin_VV
		sprite.play("Running")
		sprite.flip_h = false
	elif is_running_left && is_on_floor():
		velocity.x = -Runnin_VV
		sprite.play("Running")
		sprite.flip_h = true
	elif direction != 0:
		velocity.x = direction * SPEED
		if sprite.animation != "Walk" and is_on_floor():
			sprite.play("Walk")
	else:
		velocity.x = 0
		if is_on_floor() and sprite.animation != "idle":
			sprite.play("idle")
	
	if Input.is_action_pressed("moon_walk") && is_on_floor():
		if direction != 0 and rc.is_colliding():
			sprite.play("Walk")
			velocity.x = -MOONWALK_SPEED * direction
		else:
			velocity.x = -MOONWALK_SPEED * sign(velocity.x)
	
	if Input.is_action_just_pressed("ui_left"):
		sprite.flip_h = true
	elif Input.is_action_just_pressed("ui_right"):
		sprite.flip_h = false
	
	move_and_slide()
1 Like

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