Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

General technique for excluding edge collisions with BoxCollider2Ds

Discussion in 'Physics' started by Aniki219, Jun 20, 2022.

  1. Aniki219

    Aniki219

    Joined:
    Feb 21, 2015
    Posts:
    2
    I'm projecting a BoxCast2D at the location where I want to move my player. Let's say the player is standing on the ground and we place a BoxCast in front of them at a position that should be free from collisions. The bottom edge of the BoxCast will be resting exactly on the edge of the ground's collision box and will therefore count it as a collision with a solid at the desired location- thus it says we can't move the player there, there's a collision.

    Surely this is a common situation, is there a simple way to deal with this? I want a BoxCast that ignores edge collisions. I don't think it's okay to just shrink the size of the BoxCast by a tiny amount, that seems to sometimes allows the player to get stuck in walls.

    My movement engine is quite a bit more complicated than what I've described, I'm using raycasts to determine where to attempt to place the player, then using a boxcast at that location to make sure it's "safe" (free from collision). The problem is that locations that are safe are getting marked as unsafe because the collider will be resting on the ground or precisely against a wall. The raycast may say "there is a wall 4 units to the right" and therefore we should move the player at most 4 units to the right, but now their collider's edge overlaps the wall's edge. How do people handle this?

    collisionIssue.png
     
  2. Aniki219

    Aniki219

    Joined:
    Feb 21, 2015
    Posts:
    2
    I have come up with what seems to be a working solution, perhaps it will help someone in the future or perhaps I've deeply over-complicated this...

    I need the raycasts to be emitted from within the player's collider because if any raycast has a length of 0 it will not return the normal of the surface it collided with (it just returns <0, 0>).

    However, I cannot have the boxcast that checks if the final position is "safe" have a size that is:

    1) As "sunken into" the collider as the raycast origins are (the red box in my diagram below) because then we can have a situation where some raycasts have a length of 0 if the collider is perfectly pressed up against another.

    2) As big as the bounding box of the player (the blue box) or else the problem I was having occurs where the player is placed exactly such that this blue box will be just touching the surface we are colliding with and thus will say that location is invalid because there is a wall there.

    So instead I made the "safety check" box halfway between these sizes. When walking up a slope for example (the red slope in the diagram) the blue box is where the player will be positioned, the orange box will be used to check if there are any collisions at this location (like maybe there is a low ceiling here and the player doesn't actually fit), and the red box is where the raycasts are emitted which are still "deeper" than the orange box so that raycasts never have a length of 0 (which causes them to "glitch out" and return incorrect values).

    atlascollider.png

    If this helps anyone then great, but at least rubber-ducking here has helped me