Animate a whole sheet while holding a key

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

I want my character to lie down on the floor or a simple crouch, and when I hold my down key, it will only show the first frame and not the whole animation. what do I do here?
I click my down key and it is doing the first frame to going on the floor and it is stuck like that until I stop holding the down key.

Can you post your code here please as we cannot possibly help if we cannot see what could be causing the issue.

Gluon | 2022-12-20 10:32

if on_ground == true && is_attacking == false:
	if Input.is_action_pressed("ui_down") == true:
		animationPlayer.play("Hide")
		if is_attacking == false:
			velocity.x = 0
			velocity.y = 0	
		elif Input.is_action_released("ui_down") == false:
			animationPlayer.play("Stand")
			if is_attacking == false:
				animationPlayer.play("Idle")
		if Input.is_action_just_pressed("Shoot") && is_attacking == false:
			is_attacking = true
			animationPlayer.play("Shoot")
			var dart = Dart.instance()
			if sign($Position2D.position.x) == 1:
				dart.set_dart_direction(1)
			else:
				dart.set_dart_direction(-1)
			get_parent().add_child(dart)
			dart.global_position = $Position2D.global_position
				
			velocity.x = 0
			velocity.y = 0

here, Idk why the animation isnt working.

GrandeHolt | 2022-12-20 12:41

:bust_in_silhouette: Reply From: Gluon

Okay so the first thing i would say is there are some problems with the structure here. Your first line is asking to check if is_attacking == false but then you subsequently check that again multiple times in nested if blocks.

In addition you then check if the ui_down has been released and change it to standing so if you see this;

if on_ground == true && is_attacking == false:
    if Input.is_action_pressed("ui_down") == true:
        animationPlayer.play("Hide")
        if is_attacking == false:
            velocity.x = 0
            velocity.y = 0  
        elif Input.is_action_released("ui_down") == false:
            animationPlayer.play("Stand")
            if is_attacking == false:
                animationPlayer.play("Idle")

You can see you are telling it when you press down to start the animation to “hide” and when you release down to start the animation “stand” but also if is_attacking is false (which we already know it is as the first line checks this) you are asking it to play “Idle”.

You are giving multiple conflicting instructions to the computer here so it is no doubt trying to play the animation and immediately then being told to play the animation Idle.

You would want something like this

if on_ground == true && is_attacking == false  && $animationPlayer.get_current_animation() != "Hide":
    if Input.is_action_pressed("ui_down") == true:
            velocity.x = 0
            velocity.y = 0 
            animationPlayer.play("Hide")

then have a separate block like this

if on_ground == true && is_attacking == false  && $animationPlayer.get_current_animation() == "Hide":
            if Input.is_action_pressed("ui_up") == true:
                 velocity.x = 0
                 velocity.y = 0 
                 animationPlayer.play("Stand")

if you want stand to play all the way through and go to Idle you may need to connect a signal for animation finished to check if the last animation was stand then move it to Idle.

After some time tweaking, I still couldn’t find the problem, I looked through the Animations and its fine, I also tried some other codes but no success, thank you thought.

GrandeHolt | 2022-12-21 12:02