Search Unity

Trouble working with Composite Tilemap collider. eg. ClosestPoint doesn't work

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

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    I'm wanting to perform manual overlap tests of scenery. In my test scene which is random closed-rectangle walls, I have a tile-map that I use with a composite collider. Without enabling the composite, the collider works as expected, allowing me to find the closest point on it and reporting overlaps. Enabling the use of the composite collider, Physics2D calls fail with any results. My closest point test reports the point I'm testing for and I don't get any overlaps reported.

    upload_2022-6-20_19-41-22.png

    On the left side, we see the tilemap collider; the red dot shows the closest point to the agent (under the camera icon). Console shows overlaps results of the agent with the scenery and the scenery with the agent (tilemapcollider2d.overlap(...)).

    On the right, the the agent is testing positive even in space. The compositecollider doesn't report any overlaps with the agent.
     
  2. dying_sphynx

    dying_sphynx

    Joined:
    Sep 19, 2022
    Posts:
    1
    Hi!

    I had the same issue and used the following workaround. Instead of using ClosestPoint on the original
    TilemapCollider2D
    , I used that method on its
    composite
    property. Like this:

    Code (CSharp):
    1. Vector2 closestPoint = tileMapCollider.composite.ClosestPoint(point2D);
    I suspect it works like this because the original collider doesn't have
    bounds
    set up properly (I can see in the debugger that they are all zeros).
     
    SoftwareGeezers likes this.
  3. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    That works. Nice one!

    So instead of accessing the CompositeCollider2D component, you access the TilemapCollider2D.composite component. Which is counter-intuitive as to use the composite function of the tilemap, you have attach a Composite Collider component, but this is not the route to access it. ¯\_(ツ)_/¯