Hello Guest

Author Topic: Tilemap: Problem to detect tiles colliders  (Read 3222 times)

jmenossi

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Tilemap: Problem to detect tiles colliders
« on: December 01, 2013, 11:34:57 am »
Hi,
I'm having some trouble to detect tile colliders in a tilemap (using Unity 4.3.1 and tk2d 2.3.0)

The scenario:
-I created a new Unity project with 2D settings.
-Created a simple sprite collection with 50x50 pixel sprites and set colliders on the sprites. Using Physics2D engine.
-Created a tilemap and painted some tiles on Layer 0.
-Also, for testing, I created a tk2dSprite using the same sprite from the collection as I used to paint on the tilemap.

* Both tilemap and sprite are on the Default Unity layer.

I need to detect whether a world point is over a tile collider. I tried using Physics2D.OverlapPoint() and Physics2D.GetRayIntersection().
Both functions return correct results when used over the tk2dSprite, but they return nothing (or null) when used over the tilemap.

* If I set the Sprite collection to use Physics3D engine, I can detect colliders on the tiles with function Physics.Raycast() (it works fine)
but I need to use the 2D physics...

Any idea why the Physics2D functions are not working on a tilemap? Maybe I missed something when creating/setting the tilemap?
Or is it just a bug?

Thanks!


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap: Problem to detect tiles colliders
« Reply #1 on: December 01, 2013, 12:25:56 pm »
The tile map is built out of edge colliders when running with 2D physics. It is the recommended way of creating large colliders with Box2D. What that means is that edge colliders don't have volume, which means that you can't use the point overlap functions inside the tile map volume, as they have no volume. You can hack this to use Polygon colliders easily enough, but box2d polygon colliders must be convex and have a point limit - Unity implicitly breaks them down into many many smaller colliders behind the scenes.

The ray cast functions work best when you fire into the tile map on the same plane, rather than trying to fire from the z axis. Alternatively you can use tk2dTileMap.GetTileIdAtPosition to find out if there is a collidable tile under the position, rather than using physics - it will be considerably faster than physics.