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

Physics2D Linecast is incorrectly hitting adjacent tilemap collider - SOLVED!

Discussion in '2D' started by RidgeWare, May 20, 2018.

  1. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Hi,

    I've got a really frustrating problem which has left me scratching my head. I'm currently working on a top-down grid-based tilemap scene, but I'm having a problem with 2DLineCast (I'm experimenting with line of sight).

    If I try a 2Dlinecast from a tile with no adjacent tile collider in the start position, it works perfectly.

    But if a tile has an adjacent tile collider, it incorrectly hits early. Here's an image - the top line works perfectly, the bottom line fails:


    My question is: Can I reduce the detection bounds of TileMap Collider 2D? If I could bring the detection in slightly (on all sides of the tile) without reducing the tile image itself, I'm sure it would solve the problem. But I'm struggling to work out how. (Offset is no good, since it just moves on the X/Y axis instead of letting you reduce on all sides)

    Any advice would be greatly appreciated.
    Keith.
     
    Last edited: May 20, 2018
  2. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Would you believe it, I've fixed it!!

    For some bizarre reason, Unity Editor sets the TileMap Collider 2D just ever so slightly too far on the X & Y coordinates - in comparison to the tiles themselves.

    By changing both the X/Y offsets to -0.001 there are no longer any incorrect collisions.

    It should all be lined up automatically by default, so why I should have to manually skew the offset like this is anyone's guess.

    I would suggest this is a bug within Unity.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    Hmm, this sounds very suspicious and there isn't an offset applied by the TilemapCollider2D at all.

    If you are performing a linecast from the middle of an empty tile but it detects a neighbour tile then something is wrong. If you only move by 0.001 (1mm) and it affects such a linecast then it suggests those tiles are similarly tiny, way below a scale you should be using. What is the size of those tiles?

    If they are large then I would ask that you please create a simple reproduction project and send a bug report, posting the case here so it can be looked at quicker.
     
  4. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Thanks for responding.

    I've since done some more testing and I found the real problem -

    I was mistakenly firing the linecast using the the whole numbers for each grid position, which of course meant the line was being drawn from x.0,y.0 of each grid cell (the bottom left pixel!). Which set off the adjacent colliders.

    By adding 0.5 to the linecast Vector 2 values, it all seems to be functioning as expected now.

    I guess I was guilty of making the assumption each grid cell would automatically set a Vector 2 linecast to the centre of the cell.
     
    MelvMay likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    Glad you found the problem.