I have top down movement controls - can you help make it grid-based, please? :)

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

Hello everyone! I am working through a tutorial, and I was able to get the following movement code from fornclake’s Zelda-like Tutorial on YouTube (thank you fornclake!!).

Now I would like to add grid-based movement to this script logic, like a Pokemon clone. Can anyone help me with this? Thank you!!

Here is what I have so far:

extends KinematicBody2D

const SPEED = 70

var movedir = Vector2(0,0)
var spritedir = “down”

func _physics_process(delta):
controls_loop()
movement_loop()
spritedir_loop()

if movedir != Vector2(0,0):
	anim_switch("walk")
else:
	anim_switch("idle")

func controls_loop():
var LEFT = Input.is_action_pressed(“ui_left”)
var RIGHT = Input.is_action_pressed(“ui_right”)
var UP = Input.is_action_pressed(“ui_up”)
var DOWN = Input.is_action_pressed(“ui_down”)

if movedir.y == 0:
	movedir.x = -int(LEFT) + int(RIGHT)
if movedir.x == 0:
	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(-1,0):
spritedir = “left”
Vector2(1,0):
spritedir = “right”
Vector2(0,-1):
spritedir = “up”
Vector2(0,1):
spritedir = “down”

func anim_switch(animation):
var newanim = str(animation,spritedir)
if $anim.current_animation != newanim:
$anim.play(newanim)

Please edit your post and format the code for readability. To do that, edit the post, select the code, and click the “{ }” button in the post editor’s toolbar.

jgodfrey | 2020-06-29 20:52