I want a way to move the square in an empty direction

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

I have a 3x3 box game and I want to move the box to the empty place.
I use the walls of staticbody2d type boxes
The boxes I move are kinematicbody2d
But I have not found a suitable way to make this idea

Please help. Thank you

The code for the box that moves

extends KinematicBody2D

var vel = Vector2()
var speed = 1000
var xR = true
var xL = true
var yT = true
var yB = true

func _physics_process(delta):

if $CollisionL/Btn.pressed:
	if yT:
		translate(Vector2(0, -speed * delta))
	if yB:
		translate(Vector2(0, speed * delta))
	if xR:
		translate(Vector2(speed * delta, 0))
	if xL:
		translate(Vector2(-speed * delta, 0))
var a = move_and_collide(vel).collider.name

if a == "F2" || a == "F3" || a == "F4":
	yT = false
if a == "F6" || a == "F7" || a == "F8":
	xR = false
if a == "F10" || a == "F11" || a == "F12":
	yB = false
if a == "F13" || a == "F14" || a == "F15":
	xL = false

move_and_collide(vel)

Have you tried using rays?

HoutarouOreki | 2019-07-28 16:14

My God!!!
You’ve resolved the issue at your suggestion
Thank you

mustafamax | 2019-07-28 19:31