I'm making a game for a game jam and I'm trying to do those box sliding puzzle games, the problem is, because of godot's safe margin, the body of the box will get stuck in pixel perfect holes, so i decided to make the collision of the box 1 pixel smaller and use a ray cast so it can stop when it collides with a wall
and then everything fell appart
the ray cast is innacurate, if it moves for long enough (around 1-2 seconds) it will move 1-2 pixels extra and misalign with the tile set, since it's a sliding box game, it shouldn't really misalign (and it makes the game look kinda ugly)
after that, I went to the discord of the jam and asked for some help, they said for me to use the available physics systems, so I decided to try and change from a raycast to just an area2D, but because of the fact that the function for the Area2D only goes once, now it'll get stuck in walls eternally moving if you try to move twice to the same direction, into a wall. I tried to stop the monitoring when the area2D touches the wall, but it caused the box to either: 1: only move once and never again or 2: move like a normal topdown game
I have no idea how to fix this and there's 5 days left on the jam, I really need this bug fixed, I'll leave the code for both the raycast box and the area2d box
#raycast body:
const SPEED = 300
var schmooving:bool = false
var selected:bool = false
@onready var ray:RayCast2D = get_node("ray")
func _physics_process(delta):
if not schmooving and selected:
ray.enabled = true
var frontdir = Input.get_axis("left","right")
var sidedir = Input.get_axis("up","down")
if sidedir:
ray.target_position = Vector2(0,9)*sidedir
velocity.y = SPEED*sidedir
schmooving = true
elif frontdir:
ray.target_position = Vector2(9,0)*frontdir
velocity.x = SPEED*frontdir
schmooving = true
if ray.is_colliding():
velocity = Vector2.ZERO
schmooving = false
move_and_slide()
#area2d body:
extends CharacterBody2D
const SPEED = 300
var schmooving:bool = false
@onready var vision:Area2D = get_node("Vision")
@onready var visionCol: CollisionShape2D = get_node("Vision/CollisionShape2D")
func _physics_process(delta):
if not schmooving and selected:
vision.monitoring = true
var frontdir = Input.get_axis("left","right")
var sidedir = Input.get_axis("up","down")
if sidedir:
visionCol.shape.b = Vector2(0,9)*sidedir
velocity.y += SPEED*sidedir
schmooving = true
elif frontdir:
visionCol.shape.b = Vector2(9,0)*frontdir
velocity.x += SPEED*frontdir
schmooving = true
move_and_slide()
func _on_vision_area_entered(area):
velocity = Vector2(0,0)
schmooving = false`
one thing to note is that, when I change the speed of the raycast box, the collisions work differently, on 100 speed, it works just like I want it to, on 50 or 150 it works, but the sprite flickers and distorts when standing still, on 200 it already stops working again, please help
edit: forgot to say it, but the box needs to keep moving untill it hits a wall