I followed a tutorial that did not let my play move for some reason, but with help from the comments the movement works fine, but movement animations will not play. Help?
extends CharacterBody2D
const speed = 100
var current_dir = "none"
func _ready():
$AnimatedSprite2D.play("front_idle")
var direction : Vector2 = Vector2.ZERO
func _physics_process(delta):
direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down").normalized()
if direction:
velocity = direction * speed
else:
velocity = Vector2.ZERO
move_and_slide()
func play_anim(movement):
var dir = current_dir
if dir == "right":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("side_walk")
elif movement == 0:
$AnimatedSprite2d.play("side_idle")
if dir == "left":
$AnimatedSprite2d.flip_h = true
if movement == 1:
$AnimatedSprite2d.play("side_walk")
elif movement == 0:
$AnimatedSprite2d.play("side_idle")
if dir == "up":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("back_walk")
elif movement == 0:
$AnimatedSprite2d.play("back_idle")
if dir == "down":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("front_walk")
elif movement == 0:
$AnimatedSprite2d.play("front_idle")
you didnt add " play_anim(movement) " in your physics_process function, that’s why. the code will be ignored because the function is not getting called so do this
extends CharacterBody2D
const speed = 100
var current_dir = "none"
func _ready():
$AnimatedSprite2D.play("front_idle")
var direction : Vector2 = Vector2.ZERO
func _physics_process(delta):
play_anim(movement) # calls the function
direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down").normalized()
if direction:
velocity = direction * speed
else:
velocity = Vector2.ZERO
move_and_slide()
func play_anim(movement):
var dir = current_dir
if dir == "right":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("side_walk")
elif movement == 0:
$AnimatedSprite2d.play("side_idle")
if dir == "left":
$AnimatedSprite2d.flip_h = true
if movement == 1:
$AnimatedSprite2d.play("side_walk")
elif movement == 0:
$AnimatedSprite2d.play("side_idle")
if dir == "up":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("back_walk")
elif movement == 0:
$AnimatedSprite2d.play("back_idle")
if dir == "down":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("front_walk")
elif movement == 0:
$AnimatedSprite2d.play("front_idle")
but i recommend putting that line of code in the process function because if you put it on physics process, you can get a little bit of performance loss:
extends CharacterBody2D
const speed = 100
var current_dir = "none"
func _ready():
$AnimatedSprite2D.play("front_idle")
var direction : Vector2 = Vector2.ZERO
func _process(delta: float) -> void:
play_anim(movement)
func _physics_process(delta):
direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down").normalized()
if direction:
velocity = direction * speed
else:
velocity = Vector2.ZERO
move_and_slide()
func play_anim(movement):
var dir = current_dir
if dir == "right":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("side_walk")
elif movement == 0:
$AnimatedSprite2d.play("side_idle")
if dir == "left":
$AnimatedSprite2d.flip_h = true
if movement == 1:
$AnimatedSprite2d.play("side_walk")
elif movement == 0:
$AnimatedSprite2d.play("side_idle")
if dir == "up":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("back_walk")
elif movement == 0:
$AnimatedSprite2d.play("back_idle")
if dir == "down":
$AnimatedSprite2d.flip_h = false
if movement == 1:
$AnimatedSprite2d.play("front_walk")
elif movement == 0:
$AnimatedSprite2d.play("front_idle")