0 votes

So I have recently (literally this morning) gotten back into coding. I have a little experience with Lua, and I wanted to try out Godot. I'm making a walking animation and I have no idea how to do that. I have been looking for hours. Any help is appreciated!

if Input.is_action_pressed("move_up"):
    vel.y -= 1
    facingDir = Vector2(0, -1)
    $LilBoiTexture.texture = load("res://LilBoiAssets/LilBoiBackward.png")

    $LilBoiTexture.texture = load("res://LilBoiAssets/LilBoiBackward2.png")

this is what I have so far :D

in Engine by (12 points)

2 Answers

+1 vote

I recommend taking a look at the Godot docs, They have a tutorial of exactly what you're looking for :)
https://docs.godotengine.org/en/stable/tutorials/2d/2d_sprite_animation.html

by (55 points)

I found this but I'm running into an issue where I don't know how to stop the animation. Or rather where to stop it.

    # inputs
if Input.is_action_pressed("move_up"):
    vel.y -= 1
    facingDir = Vector2(0, -1)
    $LilBoiSprite.play("WalkBack")


if Input.is_action_pressed("move_down"):
    vel.y += 1
    facingDir = Vector2(0, 1)
    $LilBoiSprite.play("WalkFor")


if Input.is_action_pressed("move_left"):
    vel.x -= 1
    facingDir = Vector2(-1, 0)
    $LilBoiSprite.play("WalkLeft")


if Input.is_action_pressed("move_right"):
    vel.x += 1
    facingDir = Vector2(1, 0)
    $LilBoiSprite.play("WalkRight")

I dont know where to put the $animationsprite.stop()

0 votes

Simple :) create an "else:" statement at the bottom of your if statements. Your code should look like this:

if Input.is_action_pressed("move_up"):
    vel.y -= 1
    facingDir = Vector2(0, -1)
    $LilBoiSprite.play("WalkBack")


if Input.is_action_pressed("move_down"):
    vel.y += 1
    facingDir = Vector2(0, 1)
    $LilBoiSprite.play("WalkFor")


if Input.is_action_pressed("move_left"):
    vel.x -= 1
    facingDir = Vector2(-1, 0)
    $LilBoiSprite.play("WalkLeft")


if Input.is_action_pressed("move_right"):
    vel.x += 1
    facingDir = Vector2(1, 0)
    $LilBoiSprite.play("WalkRight")

else:
    $animationplayer.stop()

so what your code is saying is " if whatever input I pressed is true, play animation.
otherwise if I'm not pressing an input, stop my animation."

Also I recommend changing your other "if" statements under your first "if" statement to "elif instead. This will allow your code to override your previous button presses and keep your animations from not playing properly. Hope That solved your problem! :)

by (55 points)

Whoops, meant to hit the comment button, sorry about that.

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.