Attention | Topic was automatically imported from the old Question2Answer platform. | |
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”)`