Jumping Animation Works, but Now There's Another Issue

Godot Version

4.3

Question

Hi everyone, I'm trying to get my jumping animation to work, but after getting it to work, I'm encountering an issue. How can I fix this?

This is the video I recorded while testing the game (sorry I uploaded the external link because I can't upload the video here). https://www.youtube.com/watch?v=e-HXxSzfgd0

Code
extends CharacterBody3D

@onready var camera_mount = $camera_mount
@onready var animation_player = $visuals/mixamo_base/AnimationPlayer
@onready var visuals = $visuals

var SPEED = 3.0
const JUMP_VELOCITY = 4.5

var running = false
var is_jumping = false # ตัวแปรเพื่อติดตามสถานะการกระโดด
var is_mouse_captured = true # ตัวแปรเก็บสถานะการจับเมาส์w

@export var sens_horizontal = 0.2
@export var sens_vertical = 0.2
@export var walking_speed = 3.0
@export var running_speed = 5.0

func _ready() → void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _input(event: InputEvent) → void:
# Handle mouse motion for rotation
if event is InputEventMouseMotion and is_mouse_captured:
rotate_y(deg_to_rad(-event.relative.x * sens_horizontal))
camera_mount.rotate_x(deg_to_rad(-event.relative.y * sens_vertical))

# Handle ESC key to toggle mouse mode
if Input.is_action_just_pressed("ESC"):
	if is_mouse_captured:
		Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		is_mouse_captured = false
	else:
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
		is_mouse_captured = true

func _physics_process(delta: float) → void:
if is_on_floor():
if Input.is_action_just_pressed(“ui_accept”):
is_jumping = true
else:
is_jumping = false
if is_jumping:
animation_player.play(“Jump”)
else:
animation_player.play(“idle”)
# Handle running
if Input.is_action_pressed(“run”):
SPEED = running_speed
running = true
else:
SPEED = walking_speed
running = false

# Add gravity.
if not is_on_floor():
	velocity += get_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.
var input_dir := Input.get_vector("left", "right", "forward", "bacward")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

if direction:
	if running:
		if animation_player.current_animation != "running" and not is_jumping:
			animation_player.play("running")
	else:
		if animation_player.current_animation != "walking" and not is_jumping:
			animation_player.play("walking")
		
	# Rotate visuals toward the movement direction
	visuals.look_at(position + direction)

	# Apply movement velocity
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	# Play idle animation if not moving
	if animation_player.current_animation != "idle" and not is_jumping:
		animation_player.play("idle")
	# Decelerate smoothly when not moving
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)

# Move the character
move_and_slide()

The problems is this:

if is_jumping:
    animation_player.play(“Jump”)
else:
    animation_player.play(“idle”)

Everytime frame you dont jump you try to play idle, which overrides your other animation. Remove the else statement and try again:

if is_jumping:
animation_player.play(“Jump”)

your code is getting messy and difficult to follow. I recommend to look up state machines on youtube, they will make your life so much easier

2 Likes

Thank you very much for your advice.

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