x


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?

more ▼

asked Nov 24 '09 at 08:47 PM

user-297 (google) gravatar image

user-297 (google)
571 22 27 36

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Nov 24 '09 at 08:55 PM

Ehren gravatar image

Ehren
4.2k 35 43 77

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 25 '09 at 05:03 AM

Gabriel gravatar image

Gabriel
32 1 1 3

(comments are locked)
10|3000 characters needed characters left

Gabriel, can you please explain a bit about selective raycasting? I can't find anything about it in the docs.

more ▼

answered Dec 15 '09 at 12:37 AM

Goody! gravatar image

Goody!
516 24 31 46

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.

Dec 15 '09 at 12:15 PM Bampf

I added the description - along with a code example to Gabriels answer.

Dec 16 '09 at 09:26 AM AngryAnt ♦♦

Excellent! Thank you both!

Dec 17 '09 at 02:25 AM Goody!
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1882
x1679
x150

asked: Nov 24 '09 at 08:47 PM

Seen: 4974 times

Last Updated: Dec 16 '09 at 12:36 PM