简体   繁体   English

如何在 Unity(2D)中忽略对象上的碰撞

[英]How to ignore colliding on an object in Unity (2D)

I have 5 objects in my game and 2 "frames".我的游戏中有 5 个对象和 2 个“框架”。 I would like 1 of the objects to collide with the inner frame, but not the 4 another one.我希望其中 1 个对象与内部框架发生碰撞,但不希望 4 个与另一个对象发生碰撞。 I have attached box collider to the frames, and circle colliders to the 5 objects.我已将盒子对撞机附加到框架上,并将圆形对撞机附加到 5 个对象上。 My problem is that I don't want the 4 objects to collide with the inner frame just with the outer one.我的问题是我不希望 4 个对象仅与外部对象发生碰撞。 Is there a solution for my problem?我的问题有解决方案吗?

For anyone who couldn't find a solution for this problem: If your child object's collider is a trigger and your parent object's collider is not, then you can check the collisions in the OnCollisionEnter method.对于无法解决此问题的任何人:如果您的子对象的碰撞器是触发器,而您的父对象的碰撞器不是,那么您可以在 OnCollisionEnter 方法中检查碰撞。 If you still want to do something with other collider, then you can do it in the OnTriggerEnter method.如果您仍然想对其他对撞机做某事,那么您可以在 OnTriggerEnter 方法中进行操作。 That seperates two colliders into two different functions.这将两个对撞机分成两个不同的功能。 This helped me because my child object was a trigger, I hope this solution helps others.这对我有帮助,因为我的子对象是一个触发器,我希望这个解决方案可以帮助其他人。

This is the page, from the official guide, that explains how to use the Collision Matrix.这是官方指南中的页面,它解释了如何使用碰撞矩阵。 This is the right way to approach your problem: https://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html这是解决您的问题的正确方法: https ://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html

Basically, you go under Edit->Project Settings->Physics , and you define the rules for the layers' collision.基本上,您可以在Edit->Project Settings->Physics下,定义图层碰撞的规则。 Then, you just need to assign a layer to each object.然后,您只需为每个对象分配一个图层。

For those having this problem, It appears to be that if your parent has a RidgidBody any child collider will trigger the collision.对于那些有这个问题的人,似乎如果你的父母有一个 RidgidBody 任何子碰撞器都会触发碰撞。

I solved this by adding a RidgidBody to my child, this avoids the inheritance from the parent.我通过为我的孩子添加一个 RidgidBody 解决了这个问题,这避免了来自父母的继承。

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

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