CircleCollider2D is not registering collisions with some tiles in a TileMapCollider2D

自闭症网瘾萝莉.ら 提交于 2021-02-08 04:32:16

问题


(I've asked this question over at Unity Answers too, but it can sometimes take a while to get a response there, if I get an answer there I'll put it here too.)

I'm making a tile based game that uses Grid > Tilemap with cell layout as Hexagon.

The player can move a certain distance based on movement level. My plan is to highlight valid movement spaces in green and invalid in red. To do this I've got a child GameObject called MovementRange that has a CircleCollider2D attached with it's radius matching the player's movement level. All settings as default.

The TileMap uses a TileMapCollider2D. As each tile on the grid is smaller than the actual grid hexagon this means that each tile is separated by a gap. The TileMapCollider2D correctly assigns each tile a hexagonal collider. All settings as default. The tilemap also has a Rigidbody2D set as Kinematic and Use Full Kinematic Contacts checked.

This is the function used to highlight the tiles in range:

void HighlightCellsInRange(Collision2D collision)
 {
     Vector3 hitPosition = Vector3.zero;
     ContactPoint2D[] contacts = new ContactPoint2D[100];
     int contactCount = collision.GetContacts(contacts);

     for (int i = 0; i < contactCount; i++)
     {
         Vector3Int tile = _tileMap.WorldToCell(contacts[i].point);
         _tileMap.SetTile(tile, _greenTile);
         _tilesInRange.Add(tile);
     }
 }

This works correctly for any tile that a collision is registered for. The problem is that some tiles don't register collisions (and others have multiple collisions).

Here are some screen shots where you can see what's happening:

I have generated gizmo spheres at all of the collision contacts returned. The circle collider is selected so you can see that clearly there are missing tile collision contact points. There's 6 missing in the very center of the circle collider!

In this screen shot you can see the grid and that every tile is displaying active hexagonal colliders.

I've tried changing the settings on the circle collider and the tilemap collider but I can't understand why some of the collisions are not registering.

Any advice would be appreciated. Thanks


回答1:


I ended up sending a bug report to Unity, which they accepted (eventually).

They first tried to explain it as "It doesn't fire if the collider is already over the object". Which it wasn't anyway, and doesn't explain why the other tiles were still collided with. So I screen recorded a collider growing from 0.1 radius to full size and tracking the contact points with gizmos the entire time. You could clearly see the contact points starting out fine, then suddenly disappearing, especially on the right hand side of the tilemap as it grew.

So as it stands, collision against individual tiles is not reliable with TilemapCollider2D when the collider contacts more than a few tiles simultaneously.

Here's a GIF of the bug for those interested:



来源:https://stackoverflow.com/questions/53462798/circlecollider2d-is-not-registering-collisions-with-some-tiles-in-a-tilemapcolli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!