# GetRayIntersection

> Cast a 3D ray against the 2D Colliders in the Scene.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.6/script-reference/unityengine.md)
* **Assembly:** UnityEngine.Physics2DModule

## GetRayIntersection(Ray, float, int)

Cast a 3D ray against the 2D Colliders in the Scene.

```csharp
public static RaycastHit2D GetRayIntersection(Ray ray, float distance, int layerMask = -5)
```

### Parameters

**** (\[Ray]\(/engine/6000.6/script-reference/unityengine/ray)): The 3D ray defining origin and direction to test.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum distance over which to cast the ray.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The LayerMask filter used to select which layers to detect Colliders for.

### Returns

| Type                                                                        | Description                  |
| --------------------------------------------------------------------------- | ---------------------------- |
| [RaycastHit2D](/engine/6000.6/script-reference/unityengine/raycasthit2d.md) | The ray intersection result. |

### Remarks

This function acts as a 3D "ray cast" but specifically designed to detect 2D Colliders. It achieved this by projecting the 3D ray into 2D space and then performs the 2D intersection test, ordering the results by the Z direction of the original 3D ray.

**NOTE:** All results are sorted into ascending distance order i.e. the first result is the closest.

## GetRayIntersection(Ray, float, List\<RaycastHit2D>, int)

Cast a 3D ray against the 2D Colliders in the Scene.

```csharp
public static int GetRayIntersection(Ray ray, float distance, List<RaycastHit2D> results, int layerMask = -5)
```

### Parameters

**** (\[Ray]\(/engine/6000.6/script-reference/unityengine/ray)): The 3D ray defining origin and direction to test.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum distance over which to cast the ray.**** (\[List\<RaycastHit2D>]\(https\://learn.microsoft.com/dotnet/api/system.collections.generic.list-1)): The list to receive the results.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The LayerMask filter used to select which layers to detect Colliders for.

### Returns

| Type                                                       | Description                                  |
| ---------------------------------------------------------- | -------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The number of RaycastHit2D results returned. |

### Remarks

This function acts as a 3D "ray cast" but specifically designed to detect 2D Colliders. It achieved this by projecting the 3D ray into 2D space and then performs the 2D intersection test, ordering the results by the Z direction of the original 3D ray.

**NOTE:** All results are sorted into ascending distance order i.e. the first result is the closest.

## GetRayIntersection(Ray, float, int, Allocator)

Cast a 3D ray against the 2D Colliders in the Scene.

```csharp
public static NativeArray<RaycastHit2D> GetRayIntersection(Ray ray, float distance, int layerMask = -5, Allocator allocator = Allocator.Temp)
```

### Parameters

**** (\[Ray]\(/engine/6000.6/script-reference/unityengine/ray)): The 3D ray defining origin and direction to test.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum distance over which to cast the ray.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The LayerMask filter used to select which layers to detect Colliders for.**** (\[Allocator]\(/engine/6000.6/script-reference/unity/collections/allocator)): The memory allocator to use for the results. This can only be Allocator.Temp, Allocator.TempJob or Allocator.Persistent.

### Returns

| Type                                                                                            | Description                                                                                        |
| ----------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| [NativeArray\<RaycastHit2D>](/engine/6000.6/script-reference/unity/collections/nativearray1.md) | The NativeArray that contains the results. This must be disposed of unless it contains no results. |

### Remarks

This function acts as a 3D "ray cast" but specifically designed to detect 2D Colliders. It achieved this by projecting the 3D ray into 2D space and then performs the 2D intersection test, ordering the results by the Z direction of the original 3D ray.

**NOTE:** All results are sorted into ascending distance order i.e. the first result is the closest.
