0 votes

Hi, I'm relatively new to the engine, and I was wondering how to do walking animations for the player of an RPG.

Here's my code: `extends KinematicBody2D

var move_speed = 200
var acceleration = 200
var motion = Vector2()

func getinput():
motion = Vector2()
if Input.isactionpressed("ui_up"):
motion.y -= 1

if Input.is_action_pressed("ui_down"):
    motion.y += 1

if Input.is_action_pressed("ui_right"):
    motion.x += 1

if Input.is_action_pressed("ui_left"):
    motion.x -= 1
motion = motion.normalized() * move_speed

func getanim():
motion = Vector2()
var facing
up = false
var facingdown = true
if motion.x < 0:
$Sprite.play("Horizon")
$Sprite.flip
h = true
elif motion.x > 0:
$Sprite.play("Horizon")
$Sprite.fliph = false
else:
$Sprite.play("Horizon
Idle")

if motion.y > 0:
    facing_down = true
    $Sprite.play("Down")
elif motion.y < 0:
    facing_up = true
    $Sprite.play("Up")
else:
    if facing_down:
        $Sprite.play("Down_Idle")
    elif facing_up:
        $Sprite.play("Up_Idle")

func physicsprocess(delta):
getinput()
getanim()
motion = move
and_slide(motion)

func onVisibilityNotifier2Dscreenexited():
print("Player off screen")`

in Engine by (12 points)

1 Answer

0 votes
if v.y != 0 and not is_on_floor():
    $Sprite.animation = 'jump'
elif v.x != 0:
    $Sprite.animation = 'walk'
else:
    $Sprite.animation = 'idle'

$Sprite.play()

This is what my code looks like for a similar situation. Change the animation variable in your AnimatedSprite node to the one that you want to use, and then play the animation at the end of _physics_process with .play(). Of course this doesn't include flip_h because i was using a simple sprite that didn't need to be flipped, but I hope my example helps.

by (26 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.