What do i need to change in my code to make my jump animation play?

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

extends KinematicBody2D

var movement = Vector2();
var up = Vector2(0, -1);
var SPEED = 250;
var AttackPoints = 2;
var isAttacking = false;
var GRAVITY = 30;
var JUMPFORCE = -900
var velocity = Vector2(0,0)
onready var animationPlayer = $AnimationPlayer

func _process(delta):

if Input.is_action_pressed("right") && isAttacking == false :
	movement.x = SPEED;
	$Sprite.flip_h = false;
	$AnimationPlayer.play("Running")
elif Input.is_action_pressed("left") && isAttacking == false :
	movement.x = -SPEED;
	$Sprite.flip_h = true;
	$AnimationPlayer.play("Running")
else:
	movement.x = 0;
	if isAttacking == false:
		$AnimationPlayer.play("Idle");

velocity.y = velocity.y + GRAVITY
		
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMPFORCE
	$AnimationPlayer.play("jump")
	
velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.2)

if Input.is_action_just_pressed("a") && AttackPoints == 2:
	$AttackResetTimer.start();
	isAttacking = true;
	$AnimationPlayer.play("Attack 1");
	AttackPoints = AttackPoints - 1;

elif Input.is_action_just_pressed(“a”) && AttackPoints == 1:

$AttackResetTimer.start();

$AnimationPlayer.play(“Attack 2”);

isAttacking = true;

AttackPoints = AttackPoints - 1;

if Input.is_action_just_pressed("s") && AttackPoints == 2:
	$AttackResetTimer.start();
	isAttacking = true;
	$AnimationPlayer.play("Attack 2");
	AttackPoints = AttackPoints - 1;
elif Input.is_action_just_pressed("s") && AttackPoints == 1:
	$AttackResetTimer.start();
	$AnimationPlayer.play("attack 3");
	isAttacking = true;
	AttackPoints = AttackPoints - 1;
if Input.is_action_just_pressed("d") && AttackPoints == 2:
	$AttackResetTimer.start();
	isAttacking = true;
	$AnimationPlayer.play("attack 5");
	AttackPoints = AttackPoints - 1;
elif Input.is_action_just_pressed("d") && AttackPoints == 1:
	$AttackResetTimer.start();
	$AnimationPlayer.play("Attack 6");
	isAttacking = true;
	AttackPoints = AttackPoints - 1;

movement = move_and_slide(movement, up * delta);

func _on_AnimationPlayer_animation_finished(anim):
if anim == “Attack 1” || “Attack 2” || “Attack 3”|| “Attack 4” || “Attack 5”|| “Attack 6”:
isAttacking = false;
$AnimationPlayer.play(“Idle”)

	

func _on_AttackResetTimer_timeout():
AttackPoints = 2;

Could you format your code properly and specify the actual version of Godot you’re using?

exuin | 2021-02-22 16:22

Another suggestion, that may not solve this problem, but will improve your code in future: try to use a consistent format for your animation names: some have capital letters (Idle) and some don’t (jump) and I would always avoid having spaces in names when coding.

gioele | 2021-02-25 00:59

:bust_in_silhouette: Reply From: Jayman2000

Here’s what I think is happening:

# This is where the first if statement starts
if Input.is_action_pressed("right") && isAttacking == false:
    movement.x = SPEED
    $Sprite.flip_h = false
    $AnimationPlayer.play("Running")
elif Input.is_action_pressed("left") && isAttacking == false:
    movement.x = -SPEED
    $Sprite.flip_h = true
    $AnimationPlayer.play("Running")
else:
    movement.x = 0
    if isAttacking == false:
        $AnimationPlayer.play("Idle")
# This is where the first if statement ends

velocity.y = velocity.y + GRAVITY

# This is the second if statement
if Input.is_action_just_pressed("jump") and is_on_floor():
    velocity.y = JUMPFORCE
    $AnimationPlayer.play("jump")

For the sake of this example, let’s assume that the player is not attacking.

  1. The first if statement sets the animation to “Running” or “Idle”.
  2. The second if statement sets the animation to “jump”.
  3. One frame passes, and _process is called again.
  4. The first if statement sets the animation to “Running” or “Idle”.
  5. The second if statement does nothing because the player is no longer on the floor.

In the end, the animation is set to “jump” for at most one frame.

Here’s what you could do to fix it:
Add a variable called isJumping.

if Input.is_action_pressed("right") && isAttacking == false:
    movement.x = SPEED
    $Sprite.flip_h = false
    if isJumping == false:
        $AnimationPlayer.play("Running")
elif Input.is_action_pressed("left") && isAttacking == false:
    movement.x = -SPEED
    $Sprite.flip_h = true
    if isJumping == false:
        $AnimationPlayer.play("Running")
else:
    movement.x = 0
    if isAttacking == false && isJumping == false:
        $AnimationPlayer.play("Idle")

velocity.y = velocity.y + GRAVITY

if is_on_floor():
    if Input.is_action_just_pressed("jump"):
        velocity.y = JUMPFORCE
        $AnimationPlayer.play("jump")
        isJumping = true
    else:
        isJumping = false