【问题标题】:Unity 2019.3.9f1: Using TileMap.HasTile(), is this normal?Unity 2019.3.9f1:使用TileMap.HasTile(),这正常吗?
【发布时间】:2020-04-18 19:35:32
【问题描述】:

我尝试在 Unity 中使用 Tilemap,但遇到了一个奇怪的结果。我正在沿着瓷砖进行基于网格的移动,所以我有两个 TileMap。一个是有地面的基地,第二个是“碰撞”TileMap,里面有树木和岩石以及我的角色可能遇到的其他东西。

我想使用 HasTile 来检查 Collision TileMap 而不是使用碰撞器,因为我正在做基于网格的移动,部分结果会被浪费。只是,我似乎无法让我的代码识别 Collider TileMap 中的瓷砖。

我挖了一点,写了这个sn-p的代码

    Tilemap bas = GameObject.FindGameObjectWithTag("Ground").GetComponent<Tilemap>();
    Tilemap ter = GameObject.FindGameObjectWithTag("Terrain").GetComponent<Tilemap>();
    Vector3Int t1 = new Vector3Int(1, 1, 0);
    Vector3Int t2 = new Vector3Int(2, 1, 0);
    Vector3Int t3 = new Vector3Int(1, 2, 0);
    Vector3Int t4 = new Vector3Int(2, 2, 0);
    Debug.Log("Ground: " + bas.name);
    Debug.Log("Terrain: " + ter.name);
    Debug.Log("1, 1 has a ground tile: " + bas.HasTile(t1));
    Debug.Log("2, 1 has a ground tile: " + bas.HasTile(t2));
    Debug.Log("1, 2 has a terrain tile: " + ter.HasTile(t3));
    Debug.Log("2, 2 has a terrain tile: " + ter.HasTile(t4));`

粗略,我知道,但我只是想在已知条件下测试两个 TileMap,以确定我是否正确使用了 HasTile()。

我是这样设置的:

我从 (1,1) 中删除了地砖,并在 (2,2) 中的 Collision TileMap 中添加了一棵树

结果令人惊讶!它正确读取了我的 CollisionMap,(1,2) 为 false,(2,2) 为 true,它正确读取 GroundTile (2,1) 为 true,但它也将 GroundTile (1,1) 读取为 true!

我是否需要在它识别出磁贴已被擦除之前对其进行更新?这是一个错误吗?我可以不信任 HasTile() 吗?

【问题讨论】:

  • 我意识到我没有更新到最新版本的unity,所以我尝试更新。不幸的是,问题仍然存在。

标签: unity3d tile


【解决方案1】:

好的,我找到了!我花了太长时间,但我找到了答案。

基本上,我假设我的角色坐标与 TileMap 的坐标相同。这是错误的。

所以答案是将我的代码更改为:

Vector3Int t1 = new Vector3Int(1, 1, 0); t1 = bas.WorldToCell(t1);
Vector3Int t2 = new Vector3Int(2, 1, 0); t2 = bas.WorldToCell(t2);
Vector3Int t3 = new Vector3Int(1, 2, 0); t3 = ter.WorldToCell(t3);
Vector3Int t4 = new Vector3Int(2, 2, 0); t4 = ter.WorldToCell(t4);

现在效果很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多