Search Unity

{Solved} TilemapCollider2D (or different solution?)

Discussion in 'Scripting' started by sasuchi, May 4, 2020.

  1. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Hi everybody,

    Im currently working on a simulation game (similiar to settlers/anno/sim city etc.)... and quite heavily use the isometric tilemap system of unity. Two days ago I swapped the logic in my Preview system - if you select a building to construct, a new GameObject with a Tilemap & Tilemaprenderer Component will follow the mouse until placed - to work with Colliders instead of a (rather complicated/costly but working ) tile-by-tile comparison between my Preview-Tilemap and the Construction tilemap.
    -> Sprites are set to generate a physic shape (although i think this is unneccessary)
    -> Tiles are set to grid (hence ignoring the physics shape of the image)
    -> GameObjects have a TilemapCollider2D, Rigidbody2D and a CompositeCollider2D

    Now the problem I am facing is obviously the corner case, where when the player wants to build directly next to another building. So just looking whether the preview has any contacts is no good. Next thing i tried was using events OnCollisionEnter2D, OnCollisionStay2D and OnCollisionExit2D to update a flag whether the current preview is buildable. However this came to problems when multiple collisions were active, after leaving one, it would falsely set buildable true. While studying the Documentation, I found the Physics2D.Distance function and tried to keep track of relevant collision based on the returned distance (using the Physics2D.Distance function), however this wasnt working either. I was unsuccessfully looking for similiar topics, so either there is a rather intuitive solution to this or i was simply unlucky....

    The questions now, does anyone have an advice for me how to solve this edge-collision ? Or should i return to the previously working solution? The latter has a disadvantage, that the visuals of a building are tied to a single tilemap, hence i would have to save information about the placement of every building.

    Maybe someone has an even better idea how to tackle this.... i would appreciate the feedback!

    {Solution}
    I was having success using the GetContacts() Function of the Collider, which i tried before, but what i must have overseen was that I was trying to get the Contacts of the TilemapCollider2D and not of the CompositeCollider2D.
     
    Last edited: May 5, 2020