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

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
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
04-20 00:00

'layer'에 해당되는 글 3건

  1. 2013.02.28 Raycast 6
  2. 2013.02.21 NavMesh Layers (Pro only)
  3. 2013.01.15 Layer

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
, |

1) NavMesh Layer 셋팅 방법..




2) NavMesh Layer 셋팅 후 Bake 하는 방법

   (링크 : http://docs.unity3d.com/Documentation/Components/class-NavMeshLayers.html)


NavMesh Layers (Pro only)

The primary task of the navigation system is finding the optimal path between two points in navigation-space. In the simplest case, the optimal path is the shortest path. However, in many complex environments, some areas are harder to move thru than others (for example, crossing a river can be more costly than running across a bridge). To model this, Unity utilizes the concept of cost and the optimal path is defined as the path with the lowest cost. To manage costs, Unity has the concept of Navmesh Layers. Each geometry marked up as Navmesh Static will belong to a Navmesh Layer.

During pathfinding, instead of comparing lengths of potential path segments, the cost of each segment is evaluated. This is a done by scaling the length of each segment by the cost of the navmesh layer for that particular segment. Note that when all costs are set to 1, the optimal path is equivalent to the shortest path.

To define custom layers per project

  • Go to Edit->Project Settings->Navmesh Layers
  • Go to one of the user layers, and set up name and cost
    • The name is what will be used everywhere in the scene for identifying the navmesh layer
    • The cost indicates how difficult it is to traverse the NavMesh layer. 1 is default, 2.0 is twice as difficult, 0.5 is half as difficult, etc.
  • There are 3 built-in layers
    • Default - specifies the cost for everything not otherwise specified
    • Not walkable - the cost is ignored
    • Jump - the cost of automatically generated off-mesh links

To apply custom layers to specific geometry

  • Select the geometry in the editor
  • Pull up the Navigation Mesh window (Window->Navigation)
  • Go to the Object tab, and select the desired Navigation layer for that object
  • If you have Show NavMesh enabled in the Navmesh Display window, the different layers should show up in different colors in the editor.

To tell an agent what layers he can or cannot traverse

  • Go to the NavMeshAgent component of the agent's geometry
  • Modify NavMesh Walkable property
  • Don't forget to set the agent's destination property from a script

Note: Setting the cost value below 1 is not recommended, as the underlying pathfinding method does not guarantee an optimal path in this case

One good use case for using Navmesh Layers is:

  • You have a road that pedestrians (NavmeshAgents) need to cross.
  • The pedestrian walkway in the middle is the preferred place for them to go
  • Set up a navmesh layer with high cost for most of the road, and a navmesh layer with a low cost for the pedestrian walkway.
  • This will cause agents to prefer paths that go thru the pedestrian walkway.

Another relevant topic for advanced pathfinding is Off-mesh links 


반응형
Posted by blueasa
, |

Layer

Unity3D / 2013. 1. 15. 14:27

유니티에서는 Layer 관리를 인스펙터 창에서 한다.
기본적으로 Default, Transparent FX, Player 등의 기본 레이어가 있으며
사용자가 추가적으로 생성할 수 있다.

이 레이어들은 Camera, Phisycs 등에서 Mask 형식으로 주로 사용 되어진다.

Camera 에서는 culling 설정을 하여 해당 레이어를 렌더링 해서 화면에 보여줄지 말지를 설정 할 수 있으며
Phisycs 에서는 충돌체크시에 해당 레이어의 Collider 들을 계산할지 말지를 설정 할 수 있다.

ex1)

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

ex2)

int layerMask = (-1) - (1 << LayerMask.NameToLayer("Player"));

RaycastHit hit;

Ray ray = Camera.mainCamera.ScreenPointToRay( screenPos ); // screenPos는 화면상 클릭 좌표

bool result = Physics.Raycast( ray, out hit, 10000.0f, layerMask );


마지막 인자로 레이어 마스크 값이 쓰였으며 저기에 해당하는 레이어는 제외하고 충돌 검사를 한다. 

레퍼런스에는 충돌을 제외할 레이어 마스크 값이라고 적혀 있지만, 실제는 충돌 체크할 레이어 마스크를 넣어야 함.



P.s. Everything Layer = -1



반응형

'Unity3D' 카테고리의 다른 글

Sorting Layer 사용하기  (0) 2015.07.20
Player Settings  (0) 2013.01.15
간단한 Nav Mesh 예제  (0) 2012.11.21
런타임 중 텍스쳐 교체  (0) 2012.11.20
상체 애니메이션 덧붙이기(AddMixingTransform)  (2) 2012.11.16
Posted by blueasa
, |