Unity의 레이어 는 서로 다른 기능과 상호 작용할 수 있는 게임 오브젝트를 정의합니다. 레이어는 카메라 가 씬의 일부만 렌더링하고 광원 이 씬의 일부만 비추기 위해 가장 많이 사용됩니다. 그 외에도 콜라이더를 선별적으로 무시하거나 충돌을 생성하기 위해 레이캐스팅에서도 사용됩니다.
첫 번째 단계로 __게임 오브젝트__에 할당할 새 레이어를 만듭니다. 새 레이어를 만들려면 Tags and Layers 창을 엽니다(메인 메뉴: Edit > Project Settings로 이동한 다음 Tags and Layers 카테고리 선택).
비어 있는 사용자 레이어 중 하나에 새 레이어를 만듭니다. 여기서는 레이어 8을 선택합니다.
이제 새 레이어를 만들었으므로 레이어를 하나 이상의 게임 오브젝트에 할당할 수 있습니다.
각 게임 오브젝트에는 하나의 레이어만 할당할 수 있습니다.
Tags and Layers 창에서 플레이어 레이어가 레이어 8에 할당되어 있습니다.
카메라의 컬링 마스크를 사용하여 특정 레이어 하나에 있는 오브젝트를 선별적으로 렌더링할 수 있습니다. 이렇게 하려면 오브젝트를 선별적으로 렌더링할 카메라를 선택합니다.
컬링 마스크 프로퍼티에서 레이어를 선택하거나 선택 해제하여 컬링 마스크를 수정합니다.
UI 요소는 컬링되지 않습니다. 스크린 공간 캔버스 자식은 카메라의 컬링 마스크를 사용하지 않습니다.
레이어를 레이캐스팅에 사용하고 특정 레이어의 콜라이더를 무시하는 데 사용할 수 있습니다. 예를 들어 플레이어 레이어에만 레이캐스팅를 적용하고 그 외의 모든 콜라이더는 무시하고 싶을 수 있습니다.
Physics.Raycast 함수는 비트마스크를 사용하고, 각 비트에 따라 레이어 무시 여부가 결정됩니다. 레이어마스크의 모든 비트가 켜져 있으면 모든 콜라이더에 충돌합니다. 레이어마스크가 = 0이면 어떤 것도 레이와 충돌하지 않습니다.
int layerMask = 1 << 8;
// Does the ray intersect any objects which are in the player layer.
if (Physics.Raycast(transform.position, Vector3.forward, Mathf.Infinity, layerMask))
{
Debug.Log("The ray hit the player");
}
하지만, 보통은 그 반대로 플레이어 레이어에 있는 콜라이더를 제외한 모든 콜라이더에 레이캐스트를 적용하는 것을 원합니다.
void Update ()
{
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
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.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection (Vector3.forward) *1000, Color.white);
Debug.Log("Did not Hit");
}
}
레이어마스크를 Raycast 함수에 전달하지 않으면 IgnoreRaycast 레이어를 사용하는 콜라이더만 무시됩니다. 이 방법은 레이캐스팅을 적용할 때 일부 콜라이더를 무시하는 가장 간단한 방법입니다.
2017–05–08 페이지 수정됨
Unity 2017.1에서 컬링 마스크 정보 업데이트됨
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.