[英]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
.我将使用碰撞列表并在此处存储任何接触,使用OnCollisionEnter
和OnCollisionExit
过滤掉“伙伴”碰撞器。
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.