블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2731)
Unity3D (814)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (57)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (51)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
03-29 07:22

'Raycast'에 해당되는 글 1건

  1. 2013.02.28 Raycast 6

Raycast

Unity3D/Physics / 2013. 2. 28. 18:50

static function Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : boolean


Parameters

rayThe starting point and direction of the ray.
distanceThe length of the ray
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also:RaycastHit).
layerMaskLayer mask that is used to selectively ignore colliders when casting a ray.

Returns

boolean - True when the ray intersects any collider, otherwise false.

Description

Same as above using ray.origin and ray.direction instead of origin and direction.

참조1 : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html


위는 많이 사용되는 RayCast() 함수이다.

여기서 이야기 할 것은 4번째 매개변수인 Layer mask이다.

처음엔 레퍼런스를 대충 봐서 충돌을 무시할 Layer mask 인 줄 알았지만 그 반대였다.

충돌을 체크하고 싶은 Layer mask를 넣어야 한다.

Layer mask 사용방법에 대해서는 아래 참조2에 나온다.

참조2 : http://docs.unity3d.com/Documentation/Components/Layers.html


기본적으로 아래와 같이 사용하면 된다.

ex1)

1 int layerMask = (-1) - (1 << LayerMask.NameToLayer("Player")); // Everything에서 Player 레이어만 제외하고 충돌 체크함 2 Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask);


ex2)

1 int layerMask = (-1) - (1 << LayerMask.NameToLayer("Player")); 2 RaycastHit hit; 3 Ray ray = Camera.mainCamera.ScreenPointToRay( screenPos ); // screenPos는 화면상 클릭 좌표 4 bool result = Physics.Raycast( ray, out hit, Mathf.Infinity, layerMask );


ex3) 

1 int layerMask = 1 << LayerMask.NameToLayer("Player"); // ignore LayerMask 2 layerMask = ~layerMask ; // Invert LayerMask 3 RaycastHit hit; 4 Ray ray = Camera.mainCamera.ScreenPointToRay( screenPos ); // screenPos는 화면상 클릭 좌표 5 bool result = Physics.Raycast( ray, out hit, Mathf.Infinity, layerMask );


ex4)  // 추가

1 int layerMask = (1 << LayerMask.NameToLayer("Player")) + (1 << LayerMask.NameToLayer("Mob")); // ignore LayerMask(Player와 Mob 레이어 체크 무시하기 위해..)

2 layerMask = ~layerMask ; // Invert LayerMask 3 RaycastHit hit; 4 Ray ray = Camera.mainCamera.ScreenPointToRay( screenPos ); // screenPos는 화면상 클릭 좌표 5 bool result = Physics.Raycast( ray, out hit, Mathf.Infinity, layerMask );


참조2의 맨아래 보면 보면 ex3)과 같은 방식으로 사용된 걸 볼 수 있다.

충돌 제외할 Layer Mask를 설정 한 후 ~ 연산자로 Invert 시키고 있다.

처음엔 위쪽만 대충 읽어서 그냥 무시할 Layer Mask를 넣는 줄 알고 했다가 안돼서 레퍼런스가 틀린 줄 알았는데..

제대로 써있다..;;

레퍼런스도 끝까지 정독해야 된다는 걸 느끼게 해줬다..;;

가능하면 ex3) 방식으로 사용을 하자.


[추가]

여러 레이어를 제어하는 부분이 빠진 것 같아서 ex4)를 추가..


P.s. Everything Layer = -1

P.s.2. LayerName을 int로 변환할 때는 LayerMask.NameToLayer 를 사용하면 된다.

반응형

'Unity3D > Physics' 카테고리의 다른 글

오브젝트 간 충돌을 막고 싶을 때..  (0) 2012.11.05
Posted by blueasa
, |