简体   繁体   English

如何统一检查两个瓷砖地图之间的碰撞?

[英]How to check for collisions between two tilemaps in unity?

I have two tilemaps.我有两个瓷砖地图。 One can be moved with WASD (tiles are instantly moved to their new location) and I want to detect when the tiles of this tilemap collide with the tiles of another tilemap.可以使用 WASD 移动(图块会立即移动到新位置),我想检测此图块地图的图块何时与另一个图块图的图块发生碰撞。

Both have tilemapcollider2d and both are set to be triggers.两者都有 tilemapcollider2d 并且都设置为触发器。

They both have scripts attached with:他们都附有脚本:

private void OnTriggerEnter2D(Collider2D other) {
    Debug.Log("touching");
}

However it's never triggered and I have no idea why.但是它从未被触发,我不知道为什么。 Making one or both rigidbody2d's doesn't change anything either.制作一个或两个rigidbody2d 也不会改变任何东西。

You should use the tags on the colliding objects and compare for collision through the tags, like this:您应该使用碰撞对象上的标签并通过标签比较碰撞,如下所示:

private void OnTriggerEnter2D(Collider2D other) {
    if (other.gameObject.Tag == "tile"){
        Debug.Log("touching");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM