dareesome
dareesome

Reputation: 133

Animation plays only the first frame Godot

Im making a simple platformer. I have 3 movements that the player can do, run, jump and walk. The walking and jumping functions run fine but I have a problem with the run function, it seems to work but the animation only plays the first frame, which does not happen for any other animation.

extends KinematicBody2D

var speed : int = 200
var jumpforce: int = 500
var gravity: int = 1000

var vel: Vector2 = Vector2()

onready var sprite: Sprite = get_node("Sprite")
onready var anim = get_node("AnimationPlayer").get_animation("walk")
onready var anim2 = get_node("AnimationPlayer").get_animation("run")

func _physics_process(delta):
    vel.x = 0
    #movement
    if Input.is_action_pressed("move_left"):
        vel.x -= speed
        anim.set_loop(true)
        get_node("AnimationPlayer").play("walk")
        
    
    if Input.is_action_pressed("move_right"):
        vel.x += speed
        anim.set_loop(true)
        get_node("AnimationPlayer").play("walk")
        
        #return to idle
    if Input.is_action_just_released("move_right") or Input.is_action_just_released("move_left"):
        $AnimationPlayer.play("idle")
        #run
    if Input.is_action_pressed("move_left") and Input.is_action_pressed("shift"):
        vel.x -= speed
        anim2.set_loop(true)
        get_node("AnimationPlayer").play("run")
        
    if Input.is_action_pressed("move_right") and Input.is_action_pressed("shift"):
        vel.x += speed
        anim2.set_loop(true)
        get_node("AnimationPlayer").play("run")
    #physic and jump
    vel.y += gravity * delta
        
    if Input.is_action_pressed("jump") and is_on_floor():
        vel.y -= jumpforce
    
    vel = move_and_slide(vel, Vector2.UP)
    
    if vel.x < 0:
        sprite.flip_h = true
    elif vel.x > 0:
        sprite.flip_h = false

Upvotes: 1

Views: 1215

Answers (1)

timmmmmb
timmmmmb

Reputation: 786

Hi Man i am not sure if you still need Help but here is my solution:

extends KinematicBody2D

var speed : int = 200
var jumpforce: int = 500
var gravity: int = 1000

var vel: Vector2 = Vector2()

onready var sprite: Sprite = get_node("Sprite")

func _physics_process(delta):
    vel.x = 0
    #movement
        #run
    if Input.is_action_pressed("move_left") and Input.is_action_pressed("shift"):
        vel.x -= speed
        $AnimationPlayer.play("run")
    elif Input.is_action_pressed("move_right") and Input.is_action_pressed("shift"):
        vel.x += speed
        $AnimationPlayer.play("run")
    elif Input.is_action_pressed("move_left"):
        vel.x -= speed
        $AnimationPlayer.play("walk")
    elif Input.is_action_pressed("move_right"):
        vel.x += speed
        $AnimationPlayer.play("walk")
        #return to idle
    else:
        $AnimationPlayer.play("idle")
    

    #physic and jump
    vel.y += gravity * delta
        
    if Input.is_action_pressed("jump") and is_on_floor():
        vel.y -= jumpforce
    
    vel = move_and_slide(vel, Vector2.UP)
    
    if vel.x < 0:
        sprite.flip_h = true
    elif vel.x > 0:
        sprite.flip_h = false

The problem was that you had a bunch of if statements and every tick both the run and walk if statements were true. So for example you press left and shift. The walk left if statement is true and the animation gets set to walk. Then the run if statement is also true and your animation gets set to run. The next frame the walk animation if statement is true again which means it gets set to the walk animation again. Then the run animation is true which sets the animation to the run animation but because we are switching the animation it sets the frame to 0 and starts the animation again.

Upvotes: 1

Related Questions