How to create and use Layers in Unity - Unity Engine Tutorial 50

Опубликовано: 25 Сентябрь 2018
на канале: ChidresTechTutorials
4,212
73

How to create and use Layers in Unity - Unity Engine Tutorial 50
- Layers are the way of grouping game objects to control operations like rendering, light illumination, physics interactions, Ray casting etc.

Viewing Tags & Layers Manager:
Edit menu – Project Settings – Tags & Layers
Note: There are 32 layers; first 8 are reserved for specific purpose they are built in layers
and remaining are user layers.

Adding new layer:
Click on Layers - Click on the first empty user layer - Name the layer
Note:Layers can be renamed

Assigning layers:
Select a game object - Click on the Layer drop down menu - Click on the required layer name

Layer dropdown in the tool bar:
Used to control which layer objects must be displayed in the scene view or not

Layers are used to control:
Rendering: Which layer objects a camera must render andmust not render
Camera - Culling mask- Check/uncheck the layer(s)

Light illumination: Which layer objects must be illuminated by this light and not illuminated
Light - Culling mask- Check/uncheck the layer (s)

Physics Interactions: Which layer objects must interact with which layer objects
Edit- Project Settings - Physics - Uncheck respective checkbox

Raycasting:
Which layer objectsmustbe hit by a Ray and not

using UnityEngine;

//Attach it to camera or player
public class RayCaster : MonoBehaviour {

int layerIndex =0; // Default layer is at 0

void Update () {

int layermask = 1 << layerIndex;

// except objects within the layer index, all other objects must hit by the ray
layermask = ~layermask;

RaycastHit hit;

if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out hit, Mathf.Infinity, layermask)) {
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.green);
} else {
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * 1000, Color.red);
}

}
}

Shoot on Click:
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;

publicclassRayCaster : MonoBehaviour
{

intlayerIndex = 0; // Default layer is at 0

voidUpdate()
{

intlayermask = 1 <<layerIndex;

// except objects within the layer index, all other objects must hit by the ray
layermask = ~layermask;

RaycastHit hit;

if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layermask))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.green);
if (Input.GetMouseButtonDown(0))
{
Destroy(hit.transform.gameObject);
}
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.red);
}

}
}

Note:
- repalce < with less-than symbol.
- replace > with greater-than symbol.


=========================================

Follow the link for next video:
Unity Tutorial 51 - How to Create and Use Tags in Unity | Working with Tags in Unity
   • How to create and use Tags in Unity -...  

Follow the link for previous video:
Unity Tutorial 49 - Introduction to Tags & Layers Manager in Unity
   • Unity Tags and Layers Manager - Unity...  

=========================================

Unity Tutorials Playlist:-
   • Unity Engine Tutorials  

=========================================
Watch My Other Useful Tutorials:-

Unity Lighting Tutorials Playlist:-
   • Unity Lighting Tutorials  

Unity Camera Tutorials Playlist:-
   • Unity Camera Tutorials  

Unity Particle System Tutorials Playlist:-
   • Unity Particle System Tutorials  

Unity Physics System Tutorials Playlist:-
   • Unity Physics System Tutorials  

Unity C-Sharp Scripting Tutorials Playlist:-
   • Unity C# Scripting Tutorials  

=========================================

Subscribe to our YouTube channel:-
   / chidrestechtutorials  

Visit our Website:-
https://www.chidrestechtutorials.com

=========================================
Hash Tags:-
#ChidresTechTutorials #Unity #UnityTutorial