首页 > 解决方案 > 当第二层在第一层和第三层之间时,如何避免它们之间的碰撞?Unity 2D C#

问题描述

我有 3 层:cartrackoutOfTrack层。在cartrack下是outOfTrack层。当汽车轨道上时,会检测到汽车outOfTrack之间的碰撞,所以我想以某种方式避免它。
如何避免在汽车轨道上时检测汽车outOfTrack之间的碰撞,并且仅在汽车接触outOfTrack时才检测它?

标签: c#unity3dcollision-detectiongame-physicsgame-development

解决方案


Go to Edit->ProjectSettings->Physics. There you have the collision matrix. With the checkboxes you define which layer collides with which.

enter image description here

Edit: The collision matrix isn't supposed to change all existing contacts in realtime. If we were to do this, we'd have to re-evaluate every single contact in the world which could be very expensive and is easily abused. Check this. So you can try to change the collision matrix with code, you can probably do so, however I would suggest to handle the collisions thinking of them in terms of space, not time.

By this I mean that you try to detect the collision not at the time when your car is out of track, but, with the corresponding layers and colliders, detect the collision where its out of track.

If you are still into changing the collision matrix in runtime you can try check out the docs


推荐阅读