I followed a Zelda like tutorial from fornclakes on youtube with modification to include diagonal movements, the physics process is using delta. Let me paste the code, my edits are not very pretty but this is my first week learning to code so apologies in advance.
Also sorry I haven't included comments to make navigating easier.
extends KinematicBody2D
var SPEED = 60
var movedir = Vector2(0,0)
var spritedir = "Down"
var currentposition = Vector2()
func _input(event):
if event.is_action_pressed("ui_cancel"):
get_tree().quit()
func _physics_process(delta):
controls_loop()
movement_loop()
spritedir_loop()
if is_on_wall():
if movedir.x !=0 and movedir.y !=0:
anim_switch("Walk")
if move_and_collide(Vector2(0,0)) and movedir.x !=0 or movedir.y !=0:
anim_switch("Idle")
Input.action_release("Player_Left")
Input.action_release("Player_Right")
else:
anim_switch("Idle")
elif movedir !=Vector2(0,0):
anim_switch("Walk")
else:
anim_switch("Idle")
func controls_loop():
var LEFT = Input.is_action_pressed("Player_Left")
var RIGHT = Input.is_action_pressed("Player_Right")
var UP = Input.is_action_pressed("Player_Up")
var DOWN = Input.is_action_pressed("Player_Down")
movedir.x = -int(LEFT) + int(RIGHT)
movedir.y = -int(UP) + int(DOWN)
func movement_loop():
var motion = movedir.normalized() * SPEED
move_and_slide(motion, Vector2(0,0))
func spritedir_loop():
match movedir:
Vector2(0,-1):
spritedir = "Up"
Vector2(1,-1):
spritedir = "DiagUR"
Vector2(1,0):
spritedir = "Right"
Vector2(1,1):
spritedir = "DiagDR"
Vector2(0,1):
spritedir = "Down"
Vector2(-1,1):
spritedir = "DiagDL"
Vector2(-1,0):
spritedir = "Left"
Vector2(-1,-1):
spritedir = "DiagUL"
func anim_switch(animation):
var newanimation = str(animation, spritedir)
if $PlayerAnim.current_animation !=newanimation:
$PlayerAnim.play(newanimation)