stuck in animetedsprite moving animation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mhd08

so I’m trying to make the player animation in Godot, every animation seem to be okay but if I clicked to move and then i released my hand off the keyboard the player will stop but the animation will be stuck on “move” instead of going to idle, the only way it can return to idle animation is by clicking attack or by jumping, I’m using loop for the move animation i tried to turn it off but it can only show the move animation once

here is my player code (note: not full code):

extends KinematicBody2D

var velocity : Vector2

export var max_speed : int = 600
export var gravity : float = 35
export var jump_force : int = 1600
export var acceleration : int = 50
export var jump_buffer_time : int  = 15
export var cayote_time : int = 15

var jump_counter : int = 0
var jump_buffer_counter : int = 0
var cayote_counter : int = 0

func _physics_process(_delta):

    if is_on_floor():
	    cayote_counter = cayote_time
	    jump_counter = 0

    if not is_on_floor():
	    if cayote_counter > 0:
		    cayote_counter -= 1
	
    if jump_buffer_counter > 0 and jump_counter < 1:
	    jump_counter += 1
	    cayote_counter = 1

    velocity.y += gravity
    if velocity.y > 2000:
	    velocity.y = 2000

    if Input.is_action_pressed("right"):
	    velocity.x += acceleration 
	    $AnimatedSprite.flip_h = false
	    $AttackBox.scale.x = 1
	    get_node("AnimatedSprite").play("move")
    elif Input.is_action_pressed("left"):
	    velocity.x -= acceleration
	    $AnimatedSprite.flip_h = true
	    $AttackBox.scale.x = -1
	    get_node("AnimatedSprite").play("move")
    elif Input.is_action_just_pressed("jump"):
	    get_node("AnimatedSprite").play("jump")
    elif Input.is_action_pressed("attack"):
	    attack()
    else:
	    velocity.x = lerp(velocity.x,0,0.2)
	    if !$AnimatedSprite.is_playing():
		    get_node("AnimatedSprite").play("idle")

    velocity.x = clamp(velocity.x, -max_speed, max_speed)

    if Input.is_action_just_pressed("jump"):
	    jump_buffer_counter = jump_buffer_time
	    get_node("AnimatedSprite").play("jump")
:bust_in_silhouette: Reply From: Maximm

It seems like you’re using a lot of inefficient ways to make a platformer character. I would recommend watching a tutorial on how to make a platformer game and then build off of that. Here’s a pretty good tutorial series: https://www.youtube.com/watch?v=f3WGFwCduY0&list=PL9FzW-m48fn16W1Sz5bhTd1ArQQv4f-Cm

Also there’s a tutorial by the same person for Godot 4.0: https://www.youtube.com/watch?v=M8-JVjtJlIQ&list=PL9FzW-m48fn0i9GYBoTY-SI3yOBZjH1kJ&index=1

But to answer your previous question, the reason that your idle animation isn’t working is that the if statement you have in place prevents your character from switching to an idle animation.

This could be fixed like so:

    else:
       velocity.x = lerp(velocity.x,0,0.2)
       get_node("AnimatedSprite").play("idle")

Also it isn’t spelled ‘cayote’ it’s ‘coyote’

but then how would i let the attack animation work without holding the attack button

Mhd08 | 2023-06-23 12:41

I guess you could make a bool variable that goes true when you’re attacking, and then just make an if statement to make sure that you’re not. Also make sure that you set the attacking variable to false once you’ve finished attacking.

Attacking variable:

export var max_speed : int = 600
export var gravity : float = 35
export var jump_force : int = 1600
export var acceleration : int = 50
export var jump_buffer_time : int  = 15
export var cayote_time : int = 15

var jump_counter : int = 0
var jump_buffer_counter : int = 0
var cayote_counter : int = 0
var is_attacking = false # <---------------

If satement:

    else:
       velocity.x = lerp(velocity.x,0,0.2)
       if not is_attacking:
           get_node("AnimatedSprite").play("idle")

This should work in theory, but I would still really recommend watching some sort of tutorial for these kinds of things, it’ll be worth your time I swear.

Maximm | 2023-06-23 13:31