How to detect which is the fastest way to get the character out of any tile?

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

The priorities are up and down, if there’s no free space in those directions, then right or left. I want up and down work like godot’s default system with right and left (depending on which side it’s the fastest way to get the character out).

:bust_in_silhouette: Reply From: Moldor2

You’ll want to use grid-based movement with a standardized tileset (ie. all tiles are 16 * 16 pixels).

:bust_in_silhouette: Reply From: Magso

Use a raycast. Create a new function and use it to change the cast_to property and check is_colliding() and use return to stop the function when the raycast hits.

func _process(delta):

func RaycastCheck():
    $raycast.cast_to = Vector2.UP
    if $raycast.is_colliding():
        #move up
    $raycast.cast_to = Vector2.DOWN
    if $raycast.is_colliding():
        #move down
    $raycast.cast_to = Vector2.LEFT
    if $raycast.is_colliding():
        #move left
    $raycast.cast_to = Vector2.RIGHT
    if $raycast.is_colliding():
        #move right

But the priorities still are right and left and not up and down like i want.

Gonz4 L | 2020-09-25 23:55

It shouldn’t but if it only goes right try using yield(get_tree, "idle_frame") each time after setting cast_to to allow is_colliding() to return an accurate result.
Raycasts are sometimes better used in _physics_process so you can try using four raycasts for each direction and using if else statements.

Magso | 2020-09-26 00:49