The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

Hello im currently practicing on how to use godot and im working on a simple platformer but my running animation only plays one frame and the running animation plays when i jump heres my code below

extends KinematicBody2D

Defining Variables for movement and gravity

export (int) var gravity = 4000
export (int) var speed = 200
export (int) var jump_speed = -1000

Movement for button pressed

var velocity = Vector2.ZERO
onready var animation = $AnimatedSprite

func physicsprocess(delta):
velocity.x = 0

if velocity.y == 0 and velocity.x == 0:
    if is_on_floor():
        animation.play("idle")

if Input.is_action_pressed("move_right"):
    velocity.x = speed
    if is_on_floor():
        animation.play("run_animation")
        animation.flip_h = false

if Input.is_action_pressed("move_left"):
    velocity.x -= speed
    if is_on_floor():
        animation.play("run_animation")
        animation.flip_h = true

velocity.y += gravity * delta
velocity = move_and_slide(velocity,Vector2.UP)
if Input.is_action_just_pressed("jump"):
    if is_on_floor():
        velocity.y = jump_speed
Godot version 3.3 Stable
in Engine by (13 points)

2 Answers

+1 vote

You need to check if run_animation is still playing before animation.play("run_animation") again. Otherwise the animation will be re-run every frame causing it to run on only the first frame

if Input.is_action_pressed("move_right") and animation.animation != "run_animation":
velocity.x = speed
if is_on_floor():
    animation.play("run_animation")
    animation.flip_h = false
by (90 points)
0 votes

example:
var export jump_speed = 100
var export speed = 80

if Input.isactionpressed("moveright"):
velocity.x = speed
elif Input.is
actionpressed("moveleft"):
velocity.x = -speed

if isonfloor():
if Input.isactionjustpressed("jump"):
velocity.y = -jump
speed

if isonfloor():
if velocity.x == 0:
animation.play("idle")
elif velocity.x != 0:
animation.play("runanimation")
elif is
on_floor() == false:
if velocity.y < 0:
animation.play("jump")
elif velocity.y > 0:
animation.play("fall")

if velocity.x > 0:
animation.fliph = true
elif velocity.x < 0:
animation.flip
h = false

velocity.y += gravity * delta
velocity = moveandslide(velocity,Vector2.UP)

by (49 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.