How to do RPG animations?

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

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 _get_input():
motion = Vector2()
if Input.is_action_pressed(“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 get_anim():
motion = Vector2()
var facing_up = false
var facing_down = true
if motion.x < 0:
$Sprite.play(“Horizon”)
$Sprite.flip_h = true
elif motion.x > 0:
$Sprite.play(“Horizon”)
$Sprite.flip_h = 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 _physics_process(delta):
_get_input()
get_anim()
motion = move_and_slide(motion)

func _on_VisibilityNotifier2D_screen_exited():
print(“Player off screen”)`

:bust_in_silhouette: Reply From: Panda_Scientist
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.