본문 바로가기
[ Unity ]/- 유니티 실습

유니티 Single RayCast, Multi RayCast

by MRG 2024. 9. 14.
728x90
반응형

 

▣ Single Raycast: 하나의 광선을 발사하여 충돌한 첫 번째 물체를 감지합니다.

일반적으로 특정 물체와의 충돌 여부만 확인할 때 사용됩니다.

▣ Multi Raycast: 여러 물체와 충돌할 수 있는 광선을 발사하여 광선과 충돌한 모든 물체를 감지합니다. 

여러 개의 충돌 결과를 처리해야 할 때 유용합니다.

 

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

 

Unity - Scripting API: Physics.Raycast

Description Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene. You may optionally provide a LayerMask, to filter out any Colliders you aren't interested in generating collisions with. Specifyi

docs.unity3d.com

 

 

 

▣ 설명:
Ray: 광선을 정의합니다. transform.position에서 transform.forward 방향으로 광선을 발사합니다.
Physics.Raycast(): 광선을 발사하고, 충돌 여부를 확인합니다. out hit은 충돌 정보를 저장하는 데 사용됩니다.
hit.collider.gameObject.name: 충돌한 물체의 이름을 가져옵니다.

 

 

 

▣ 설명:

Physics.RaycastAll(): 광선을 발사하고, 광선이 충돌한 모든 물체를 배열로 반환합니다.
hits: 충돌한 모든 물체의 정보를 담고 있는 배열입니다.
hits.Length: 광선에 충돌한 물체의 개수를 나타냅니다.
foreach: 충돌한 모든 물체의 이름을 순차적으로 출력합니다.

Debug.DrawLine(ray.origin, hit.point, Color.red): 충돌한 지점까지 빨간색 라인을 그립니다.
Debug.DrawLine(ray.origin, ray.origin + ray.direction * maxDistance, Color.green): 충돌이 없을 경우, 최대 거리까지 초록색 라인을 그립니다.

LayerMask layerMask: 특정 레이어에 속한 물체들과만 충돌을 감지합니다. Inspector에서 원하는 레이어를 선택할 수 있습니다.
Physics.RaycastAll(ray, maxDistance, layerMask): 레이어 마스크를 적용하여, 지정된 레이어에 속한 물체들만 충돌을 감지합니다.
LayerMask.LayerToName(hit.collider.gameObject.layer): 충돌한 물체의 레이어 이름을 출력합니다.

 

 

https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html

 

Unity - Scripting API: Physics.RaycastAll

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/LayerMask.html

 

Unity - Scripting API: LayerMask

A GameObject can use up to 32 LayerMasks supported by the Editor. The first 8 of these Layers are specified by Unity; the following 24 are controllable by the user. Bitmasks represent the 32 Layers and define them as true or false. Each bitmask describes w

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/LayerMask.LayerToName.html

 

Unity - Scripting API: LayerMask.LayerToName

Note: LayerToName takes an integer argument. This argument selects the name of Layer and returns it. The layers are listed in the inspector. As an example assume User Layer 13 has a string. This string can be accessed by calling LayerToName with the value

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Debug.DrawLine.html

 

Unity - Scripting API: Debug.DrawLine

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

728x90
반응형

'[ Unity ] > - 유니티 실습' 카테고리의 다른 글

유니티 GameSettingsUIManager  (1) 2024.09.17
유니티 Camera Shake  (0) 2024.09.15
유니티 Animator, Animation, Blend Tree  (1) 2024.09.14
유니티 오브젝트 스크롤링  (1) 2024.09.13
유니티 2D Collider  (0) 2024.09.13

댓글