Layers
Graphics Reference

Layer-based collision detection

Layer-based collision detection is a way to make a GameObject collide with another GameObject that is set up to a specific Layer or Layers.

Objects colliding with their own layer
Objects colliding with their own layer

The image above shows six GameObjects (3 planes, 3 cubes) in the Scene view, and the Layer Collision Matrix in the window to the right. The Layer Collision Matrix defines which GameObjects can collide with which Layers.

In the example, the Layer Collision Matrix is set up so that only GameObjects that belong to the same layer can collide:

  • Layer 1 is checked for Layer 1 only
  • Layer 2 is checked for Layer 2 only
  • Layer 3 is checked for Layer 3 only

Change this to suit your needs: if, for example, you want Layer 1 to collide with Layer 2 and 3, but not with Layer 1, find the row for Layer 1, then check the boxes for the Layer 2 and Layer 3 colums, and leave the Layer 1 column checkbox blank.

Setting up layer-based collision detection

  1. To select a Layer for your GameObjects to belong to, select the GameObject, navigate to the Inspector window, select the Layer dropdown at the top, and either choose a Layer or add a new Layer. Repeat for each GameObject until you have finished assigning your GameObjects to Layers.
  2. In the Unity menu bar, go to Edit > Project Settings, then select the Physics category to open the Physics window.
  3. Select which layers on the Collision Matrix will interact with the other layers by checking them.
Layers
Graphics Reference
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961