# Linecast

> Casts a line segment against Colliders in the Scene.

## Definition

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

## Linecast(Vector2, Vector2, int, float, float)

Casts a line segment against Colliders in the Scene.

```csharp
public static RaycastHit2D Linecast(Vector2 start, Vector2 end, int layerMask, float minDepth, float maxDepth)
```

### Parameters

**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The start point of the line in world space.**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The end point of the line in world space.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Filter to detect Colliders only on certain layers.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): Only include objects with a Z coordinate (depth) greater than or equal to this value.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): Only include objects with a Z coordinate (depth) less than or equal to this value.

### Returns

| Type                                                                        | Description                |
| --------------------------------------------------------------------------- | -------------------------- |
| [RaycastHit2D](/engine/6000.5/script-reference/unityengine/raycasthit2d.md) | The cast results returned. |

### Remarks

A *linecast* is an imaginary line between two points in world space. Any object making contact with this line can be detected and reported. This differs from the similar *raycast* in that raycasting specifies the line using an origin and direction.

This function returns a RaycastHit2D object when the line contacts a Collider in the Scene. The *layerMask* can be used to detect objects selectively only on certain layers (this allows you to apply the detection only to enemy characters, for example). The direction of the line is assumed to extend from the start point to the end point. Only the first Collider encountered in that direction will be reported. Although the Z axis is not relevant for rendering or collisions in 2D, you can use the *minDepth* and *maxDepth* parameters to filter objects based on their Z coordinate.

Linecasts are useful for determining lines of sight, targets hit by gunfire and for many other purposes in gameplay.

Note that this function will allocate memory for the returned RaycastHit2D object.

Additionally, this will also detect Collider(s) at the start of the line.  In this case the line is starting inside the Collider and doesn't intersect the Collider surface.  This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the line vector being tested.  This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

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

Additional Resources: [LayerMask](/engine/6000.5/script-reference/unityengine/layermask.md) class, [RaycastHit2D](/engine/6000.5/script-reference/unityengine/raycasthit2d.md) class, [Physics2D.LinecastAll](/engine/6000.5/script-reference/unityengine/physics2d/linecastall.md), [Physics2D.DefaultRaycastLayers](/engine/6000.5/script-reference/unityengine/physics2d/defaultraycastlayers.md), [Physics2D.IgnoreRaycastLayer](/engine/6000.5/script-reference/unityengine/physics2d/ignoreraycastlayer.md), [Physics2D.raycastsHitTriggers](/engine/6000.5/script-reference/unityengine/physics2d/raycastshittriggers.md).

## Linecast(Vector2, Vector2, ContactFilter2D, RaycastHit2D\[])

Casts a line segment against Colliders in the Scene with results filtered by [ContactFilter2D](/engine/6000.5/script-reference/unityengine/contactfilter2d.md).

```csharp
public static int Linecast(Vector2 start, Vector2 end, ContactFilter2D contactFilter, RaycastHit2D[] results)
```

### Parameters

**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The start point of the line in world space.**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The end point of the line in world space.**** (\[ContactFilter2D]\(/engine/6000.5/script-reference/unityengine/contactfilter2d)): The contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.**** (\[RaycastHit2D\[]]\(/engine/6000.5/script-reference/unityengine/raycasthit2d)): The array to receive results.  The size of the array determines the maximum number of results that can be returned.

### Returns

| Type                                                       | Description                                                  |
| ---------------------------------------------------------- | ------------------------------------------------------------ |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | Returns the number of results placed in the `results` array. |

### Remarks

The overloads of this function with the `contactFilter` parameter can filter the returned results by the options in [ContactFilter2D](/engine/6000.5/script-reference/unityengine/contactfilter2d.md).

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

Additional Resources: [ContactFilter2D](/engine/6000.5/script-reference/unityengine/contactfilter2d.md) and [RaycastHit2D](/engine/6000.5/script-reference/unityengine/raycasthit2d.md).

## Linecast(Vector2, Vector2, ContactFilter2D, List\<RaycastHit2D>)

Casts a line segment against Colliders in the Scene with results filtered by [ContactFilter2D](/engine/6000.5/script-reference/unityengine/contactfilter2d.md).

```csharp
public static int Linecast(Vector2 start, Vector2 end, ContactFilter2D contactFilter, List<RaycastHit2D> results)
```

### Parameters

**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The start point of the line in world space.**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The end point of the line in world space.**** (\[ContactFilter2D]\(/engine/6000.5/script-reference/unityengine/contactfilter2d)): The contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.**** (\[List\<RaycastHit2D>]\(https\://learn.microsoft.com/dotnet/api/system.collections.generic.list-1)): The list to receive results.

### Returns

| Type                                                       | Description                                                 |
| ---------------------------------------------------------- | ----------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | Returns the number of results placed in the `results` list. |

### Remarks

The integer return value is the number of results written into the `results` list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the `results` list does not need to be resized, and improves garbage collection performance when the query is performed frequently.

The results can also be filtered by the `contactFilter`.

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

Additional Resources: [ContactFilter2D](/engine/6000.5/script-reference/unityengine/contactfilter2d.md) and [RaycastHit2D](/engine/6000.5/script-reference/unityengine/raycasthit2d.md).

## Linecast(Vector2, Vector2, ContactFilter2D, Allocator)

Casts a line segment against Colliders in the Scene with results filtered by [ContactFilter2D](/engine/6000.5/script-reference/unityengine/contactfilter2d.md).

```csharp
public static NativeArray<RaycastHit2D> Linecast(Vector2 start, Vector2 end, ContactFilter2D contactFilter, Allocator allocator = Allocator.Temp)
```

### Parameters

**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The start point of the line in world space.**** (\[Vector2]\(/engine/6000.5/script-reference/unityengine/vector2)): The end point of the line in world space.**** (\[ContactFilter2D]\(/engine/6000.5/script-reference/unityengine/contactfilter2d)): The contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.**** (\[Allocator]\(/engine/6000.5/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.5/script-reference/unity/collections/nativearray1.md) | The NativeArray that contains the results. This must be disposed of unless it contains no results. |

### Remarks

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

Additional Resources: [ContactFilter2D](/engine/6000.5/script-reference/unityengine/contactfilter2d.md) and [RaycastHit2D](/engine/6000.5/script-reference/unityengine/raycasthit2d.md).
