Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question CompositeCollider.OverlapPoint returns false when it should be true

Discussion in '2D' started by ChickenMission, Mar 3, 2023.

  1. ChickenMission

    ChickenMission

    Joined:
    Jul 25, 2020
    Posts:
    18
    I have a tilemap that uses an outline composite and I want to know if a rigidbody is inside the collider or not. here is the code I use:
    Code (CSharp):
    1. ground.GetComponent<CompositeCollider2D>().OverlapPoint(body.position)
    this always returns false even when I can see that it should return true. When I use this code:
    Code (CSharp):
    1.  
    2. Debug.Log(ground.GetComponent<CompositeCollider2D>().bounds + " | " + body.position);
    3. Debug.Log(ground.GetComponent<CompositeCollider2D>().bounds.Contains(body.position));
    4. Debug.Log(ground.GetComponent<CompositeCollider2D>().OverlapPoint(body.position));
    It returns
    so I know that the body is inside the tilemap, but I can't use bounding box for the main code as the tilemap isn't a perfect square (there are holes for example) so the function will return false trues.

    Does anyone know what caused this problem and have any potential solutions?
    Thanks!
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
  3. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    302
    Just want to give some insight. Do NOT use polygon if your tilemap is used as ground for the player. You will get ghost collisions. I see people recommending it as a fix, but they don’t realize it will cause this issue.
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Those aren't 'ghost collisions'. They're just collisions.
    If you have the collider set to Outline, there is no 'inside' for the collider. If you've set it to Polygon, there is.
    So for Outline there are no collisions 'inside' the collider, and for Polygon there are.
     
  5. ChickenMission

    ChickenMission

    Joined:
    Jul 25, 2020
    Posts:
    18
    I need an object with collision to be able to move freely inside it, so I can't set it to polygon. If overlap point won't work, is there another way to see if a point is inside a composite collider?
     
  6. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    302
    Unfortunately, this is not true. You just have to google to find out the answer: https://forum.unity.com/threads/composite-collider-2d-ghost-collision.923417/#post-6043406

    Please read this bit:

    "Ghost collisions don't occur when moving across a continuous surface. This is only provided by edge-chains so EdgeCollider2D or CompositeCollider2D to set Outline mode (which produces edge-chains). This is the shape type in Box2D used when using the edge or composite in outline mode.

    If you're using polygon mode then you're still producing multiple physics shapes which are not a continuous surface and are no different than multiple colliders."
     
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Bounds, like you already used.
    Or perhaps you can change it to polygon, do your OverlapPoint, then change it back?
     
  8. ChickenMission

    ChickenMission

    Joined:
    Jul 25, 2020
    Posts:
    18
    changing to polygon and back would cause a bunch of lag as physics has to be done as if its a polygon, and this function is run every frame so it would kill the game completely.

    Like I said above, bounds also don't work as they are only rectangles, not complex shapes, so it would return a bunch of false positives.

    I also tried using raycasts that point in all four directions to see if there is the collider on all four sides, and then returning true if there is, but this didn't work when in a cave in the tilemap where there are walls on all four sides (again returning false positives).

    If anyone knows any other potential solutions, please let me know, I'd love to at least give them a go!
     
  9. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    302
    You could duplicate your tilemap, add the polygon collider, remove the renderer and other components you don't need, and just use that for testing. (Set it to isTrigger) of course.
     
  10. ChickenMission

    ChickenMission

    Joined:
    Jul 25, 2020
    Posts:
    18
    I need this to work for the main game so hopefully something more efficient would be available. I guess having two colliders on the tilemap would work though
     
    TomTheMan59 likes this.