I see that Unity have layers. So can someone explain me a little bit how they used or where and when are layers are using ?
Unity uses layers for things like Raycasting, camera culling (you want the camera to only render some layers), light/projector culling, it can also be used in scripts for various things.
Most often you use layers by creating LayerMasks which you can edit in the editor, then it can be used in a Raycast for example:
var mask : LayerMask;
function Update () {
if (Physics.Raycast (transform.position,transform.forward,5,mask)) {
Debug.Log ("There is something in front of the object");
}
}
The ray will only hit if the layermask contains the layer the object, otherwise it will continue.
For example you can assign it to only hit the “Enemy” layer, or the “Ground” layer.
A common use I’ve had for layers is the use of a minimap. I basically have each object have a 2D plane above their heads that faces upwards and holds their unit icon. The first camera then turns off the icon layer. The second camera that I activate and print to the minimap area of the screen only views the icon and terrain layers, creating an awesome mobile minimap.