CircleCollider2D未在TileMapCollider2D中注册与某些图块的碰撞 [英] CircleCollider2D is not registering collisions with some tiles in a TileMapCollider2D

查看:19
本文介绍了CircleCollider2D未在TileMapCollider2D中注册与某些图块的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我也曾在Unity Answers上问过这个问题,但有时可能需要一段时间才能在这里得到答复,如果我在那里得到答案,我也会把它放在这里.)

(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.)

我正在制作一个基于图块的游戏,该游戏使用单元格布局为Hexagon的Grid> Tilemap.

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

玩家可以根据移动水平移动一定距离.我的计划是用绿色突出显示有效的移动空间,用红色突出显示无效的移动空间.为此,我有一个名为MovementRange的子游戏对象,该对象附有CircleCollider2D,其半径与玩家的运动水平相匹配.所有设置均为默认设置.

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.

TileMap使用TileMapCollider2D.由于网格上的每个图块都小于实际的网格六边形,这意味着每个图块之间都被间隙隔开.TileMapCollider2D可以为每个图块正确分配一个六边形对撞机.所有设置均为默认设置.磁贴图还将Rigidbody2D设置为Kinematic,并选中了Use Full Kinematic Contacts.

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:

我在返回的所有碰撞接触点都生成了Gizmo球体.选择了圆形对撞机,因此您可以清楚地看到缺少瓷砖碰撞接触点.圆形对撞机的正中央缺少6个!

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.

我尝试更改圆形碰撞器和tilemap碰撞器的设置,但我不明白为什么有些碰撞没有记录.

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

推荐答案

我最终向Unity发送了一个错误报告,最终他们接受了.

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

他们首先尝试将其解释为如果对撞机已经在物体上方,则不会触发".无论如何,这并没有解释为什么其他图块仍然与之碰撞.因此,我筛选了一个从0.1半径增长到完整大小的对撞机,并一直跟踪与小物件的接触点.您可以清楚地看到接触点起初很好,然后突然消失,尤其是在图块地图的右侧,随着它的增大.

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.

就目前而言,当对撞机同时接触多个贴图时,使用TilemapCollider2D对单个贴图的碰撞是不可靠的.

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

以下是对该漏洞感兴趣的人的GIF:

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

这篇关于CircleCollider2D未在TileMapCollider2D中注册与某些图块的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆