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):
RaycastCheck()
func RaycastCheck():
$raycast.cast_to = Vector2.UP
if $raycast.is_colliding():
#move up
return
$raycast.cast_to = Vector2.DOWN
if $raycast.is_colliding():
#move down
return
$raycast.cast_to = Vector2.LEFT
if $raycast.is_colliding():
#move left
return
$raycast.cast_to = Vector2.RIGHT
if $raycast.is_colliding():
#move right
return