简体   繁体   English

Unity 5.6 2D- 如何检查 2 个对撞机是否没有接触任何其他对撞机?

[英]Unity 5.6 2D- How can I check if 2 colliders isn't touching any other collider?

In Unity 5.6 C#, I know there is a way to check for if a collider is being touched by any other collider by using IsTouching.在 Unity 5.6 C# 中,我知道有一种方法可以使用 IsTouching 检查碰撞器是否被任何其他碰撞器触摸。

However, I would like to know how to group two colliders together(that are touching each other), and how to check if they both are touching any collider besides each other.但是,我想知道如何将两个对撞机组合在一起(彼此接触),以及如何检查它们是否都在接触彼此之外的任何对撞机。

I will give it a shot with the idea I mentioned in the comments (I see it is hard to understand with only the comments section).我将尝试使用我在评论中提到的想法(我认为仅通过评论部分很难理解)。

I would use a list of collisions and store any touches here, filtering out the "partner" collider using OnCollisionEnter and OnCollisionExit .我将使用碰撞列表并在此处存储任何接触,使用OnCollisionEnterOnCollisionExit过滤掉“伙伴”碰撞器。

Since both are attached to the same GameObject it is easy to filter them:由于两者都附加到同一个游戏对象,因此很容易过滤它们:

public class Collisions : MonoBehaviour
{
    // Show in the Inspector for debug
    [SerializeField] private List<Collider> colliderList = new List<Collider>();

    public bool IsTouching => colliderList.Count != 0;

    private void Awake ()
    {
        // Make sure list is empty at start
        colliderList.Clear();
    }

    private void OnCollisionEnter(Collision collision)
    {
        // Filter out own collider
        if(collision.gameObject == gameObject) return;

        if(!colliderList.Contains(collision.collider) colliderList.Add(collision.collider);
    }

    private void OnCollisionExit(Collision collision)
    {
        // Filter out own collider
        if(collision.gameObject == gameObject) return;

        if(colliderList.Contains(collision.collider) colliderList.Remove(collision.collider);
    }
}

Typed on smartphone but I hope the idea gets clear在智能手机上打字,但我希望这个想法变得清晰

暂无
暂无

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

相关问题 如何在Unity中检测Tilemap碰撞体和2D盒子碰撞体之间的碰撞 - How can I detect collisions between Tilemap Colliders and 2D box colliders in Unity 检测 2D Collider 是否从 Unity 2D 中的其他脚本触摸鼠标指针 - Detecting if 2D Collider is touching Mouse Pointer from Other script in Unity 2D 如何通过在 Unity 2D 中触摸屏幕来移动角色 - How can I move the character by touching the screen in Unity 2D 如何检查GameObject是否包含MeshRenderer但不包含任何对撞机,然后向其添加对撞机? - How can I check if a GameObject contain a meshrenderer but not contain any collider and then add a collider to it? 如何检查我是否在 Unity 中触摸了某些东西 - How to check if i`m touching something in Unity 如何通过触摸和不触摸球体碰撞器来打开和关闭灯? - How can i make the light turn on and off by touching and not touching a sphere collider? 当多个对撞机同时接触我的角色时,如何找到哪个具有相同标签的对撞机首先接触我的角色 - How to find which collider with same tag is touching my character firstly when multiple colliders are touching my character same time 如何在Unity中固定对撞机阵列中随机实例化预制件? - How can I randomly instantiate a prefab in a fixed array of colliders in Unity? 检查Unity3D中的哪个对撞机触发器 - Check which collider trigger in Unity3D 当两个碰撞器接触时仅禁用一个碰撞器 - Disable only one collider when two colliders are touching
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM