- Home /
What is the purpose of layers?
Why I want to use layer for? I know I can isolate GO (Game object) from the scene, but to do what? Why doing that?
Answer by Ehren · Nov 24, 2009 at 08:55 PM
From the docs:
Layers are most commonly used by Cameras to render only a part of the scene, and by Lights to illuminate only parts of the scene. But they can also used by raycasting to selectively ignore colliders.
Tags are similar to layers, in that you can use them to mark/group GameObjects. However, tags are "intended to identify GameObjects for scripting purposes," while layers are "used to define how Unity should render GameObjects in the Scene."
If you need to identify certain objects from your scripts (such as enemies, asteroids, walls, etc.), use Tags. If you need to control how certain GameObjects are rendered, use Layers.
Answer by Gabriel · Nov 25, 2009 at 05:03 AM
Also, selective raycasting - as in only raycasting against colliders in specific layers as opposed to raycasting against all colliders.
Example:
void TestLineOfFire () { LayerMask mask = 1 << LayerMask.NameToLayer ("Friendlies") | 1 << LayerMask.NameToLayer ("Enemies");
weapon.willHitSoftTarget = Physics.Raycast (
transform.position,
transform.forward,
weapon.maximumRange,
mask
);
}
For more information on masks and layer masks, check out the How do I use layermasks? question.
Answer by Goody! · Dec 15, 2009 at 12:37 AM
Gabriel, can you please explain a bit about selective raycasting? I can't find anything about it in the docs.
For examples see the page on Unity Layers, near the bottom: http://unity3d.com/support/documentation/Components/Layers.html All the Physics.Raycast calls take a layer mask as an optional argument. The mask lets you test for a hit against objects in only some of the layers, rather than all.
I added the description - along with a code example to Gabriels answer.