Hello Everyone,
I am currently facing the following issue:
I have a single Tilemap composed of Tiles, where some Tiles (e.g.ground) dont have colliders and some Tiles do have colliders (e.g. wall/map-limitter). The player has a gameobject, called focus, that will selects one tile adjacent to the player, corresponding to the mouse position, and with Input.GetMouseButtonUp(0) it does a Physics2D.Raycast() and should act accordingly.
With the CompositeCollider2D activated, the fence/Tile with a collider is not being hit.
With the CompositeCollider2D deactivated (hence using single colliders), the fence/Tile with a collider is being hit.
void UseAction(Vector2 point)
{
focus.SetActive(false);
RaycastHit2D hit = Physics2D.Raycast(focus.transform.position, Vector2.zero, Mathf.Infinity);
if (hit.collider != null && hit.collider.gameObject != gameObject)
{
Debug.Log(hit.collider.name);
}
else
{
GameManager._instance.tilemap.activeMap.Interact("Fist", new Vector2Int(Mathf.RoundToInt(point.x), Mathf.RoundToInt(point.y)));
}
}
Those are the ColliderSettings at the Tilemap
Response with inactive CompositeCollider2D :
And obviously there is no response when its active, instead the tile is swapped.
Are there any Solutions beside not using the CompositeCollider2D, since I dont want any phantom vertices at the map-delimiter, or using multiple Tilemaps ?
I appreciate your input.
best regards
sasuchi
No idea
This isn’t a raycast at all. It’s a degenerate ray trying to do an overlap point because it has no direction but infinite distance. It makes little sense to do that when there’s a dedicated call to do it i.e. Physics2D.OverlapPoint
I think the problem you’re having though is that you’re assuming that an outline made of edges somehow has an inside which it doesn’t. It’s not a polygon closed shape, it’s an open shape composed of edges.
You can only detect edges by intersecting the edges i.e. crossing the edges with a raycast.
This is the same for CompositeCollider2D in Outline mode as it is for an EdgeCollider2D because they both produce the same primitive edge chains in the physics system.
1 Like
Thank you for the Reply MelvMay,
Concerning you’re point about the OverlapPoint, yes you are absolutely correct, I must have overlooked that function while looking into the documentation or simply was already satisfied because what i tested worked with the ray :).
However concerning the edge detection, the given input Vector2 point is rounded to Int values and the Tile colliders are set to Grid Colliders. Hence the detection should only miss, if the corner that is pointed to is not existent.
As example, the Vector point is in both cases (0,0,0) (the slightly greyish sprite with a size of 1 is the gameobject which transform is rounded to nearest int).
On the left image its without the compositeCollider2d and on the right its with the compositeCollider2d

I would assume it would look for overlapping in the lower left corner (EDIT or maybe upper right, since 0.5f would probably be rounded to 1, if i think about it)

and not, as you also mentioned, the center of the gameobject, because then it wouldn’t detect the overlapping point in neither scenario (with/without CompositeCollider).
I hope I could make my point clear and unterstood from you completly, since Im not a native speaker.
Thanks
As I said, you cannot detect overlap with edges no matter how you use it. Edges are infinitely thin and have no area to overlap with. It doesn’t matter if you’re using ints or whatever value you use. Only polygons, capsules & circles have an area that can overlap.
For edges, you have to perform an intersection test that passes through an edge so line/ray cast that passes through it or shape-cast.
If you cast a line from the player to the center of the tile then that will tell you if you will hit an edge or not.
Okay, but then, why does it detect the Overlap with the same exact code if I am not using a CompositeCollider ? Shouldn’t it be unable regardless of the Collider of the Tile?
Because the TilemapCollider2D produces polygons which are closed shapes and do have an interior. If you set the CompositeColilder2D to Polygon it’ll be the same. You’ve set the composite to Outline which uses edges the exact same as the EdgeCollider2D.
1 Like
Ah okay thank you very much now i’ve understood your point.