简体   繁体   中英

how to avoid collision between first and third layer when second is between them? Unity 2D C#

I have 3 layers: car , track and outOfTrack layer. Under car and track is outOfTrack layer. When a car is on track the collision between car and outOfTrack is detected, so i want to avoid it somehow.
How to avoid collision detecting between car and outOfTrack when car is on track , and detecting it only when car is touching outOfTrack ?

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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