- Home /
Raycast origin overlapping surface of collider counts as a hit
I'm making a simple collision detection system using raycasts2d and polygonCollider2d. There's a few rules: all objects will not move until you drag them with your mouse. When dragging an object, it fires Raycast2DAlls from every corner of its collider in the direction the object is being moved, with the distance its being moved. With this I can detect if an object is trying to be moved inside another one and shorten the distance so it instead moves adjacent and doesn't overlap.
The problem is that once two objects are now touching, at least one of the raycasts has an origin that is directly inside the surface of another collider. it is not 'inside' the collider, but exactly along the surface. now, if I try to cast a raycast in any direction, it will detect a hit at its origin and will never move. even if I try to fire the raycast In a direction away from the surface, it will still detect a hit at the origin. the only solution I see to this problem is using an arbitrary and hacky workaround where I start the raycast a little bit ahead of where its supposed to start, but that will cause collision bugs on corners and it seems like a bad solution.
I know that naturally a ray that starts inside a collider will not hit back faces, but for some reason on my project a ray will always detect collision at its origin if inside a collider. I don't know whats new but I've never encountered this problem before.
I've also tried using multiple rays, creating new rays from each hit to continue so I can find multiple hit points for the same object's collider, but this results in an infinite loop, because the origin always returns an immediate hit so every new raycast will never progress past its origin.
Under the Edit > Project Settings > Physics2D settings you'll find a checkbox that says Queries start in colliders you'll want to uncheck that and you should be good to go.
Your answer
Follow this Question
Related Questions
raycast not colliding 1 Answer
Raycast Not Drawing In Target Direction 0 Answers
Even-odd test with raycasting 1 Answer
Use RayCasting and simulate collision physics 1 Answer
Raycast bullet collision problem 1 Answer