Legacy Documentation: Version 2017.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Physics2D.Linecast

Suggest a change

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

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function Linecast(start: Vector2, end: Vector2, layerMask: int = DefaultRaycastLayers, minDepth: float = -Mathf.Infinity, maxDepth: float = Mathf.Infinity): RaycastHit2D;
public static RaycastHit2D Linecast(Vector2 start, Vector2 end, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

Parameters

start The start point of the line in world space.
end The end point of the line in world space.
layerMask Filter to detect Colliders only on certain layers.
minDepth Only include objects with a Z coordinate (depth) greater than or equal to this value.
maxDepth Only include objects with a Z coordinate (depth) less than or equal to this value.

Returns

RaycastHit2D The cast results returned.

Description

Casts a line segment against colliders in the Scene.

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. You can use LinecastNonAlloc to avoid this overhead if you need to make linecasts frequently.

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.

See Also: LayerMask class, RaycastHit2D class, LinecastAll, LinecastNonAlloc, DefaultRaycastLayers, IgnoreRaycastLayer, raycastsHitTriggers.


public static function Linecast(start: Vector2, end: Vector2, contactFilter: ContactFilter2D, results: RaycastHit2D[]): int;
public static int Linecast(Vector2 start, Vector2 end, ContactFilter2D contactFilter, RaycastHit2D[] results);

Parameters

start The start point of the line in world space.
end The end point of the line in world space.
results The array to receive results. The size of the array determines the maximum number of results that can be returned.
contactFilter The contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.

Returns

int Returns the number of results placed in the results array.

Description

Casts a line segment against colliders in the Scene with results filtered by ContactFilter2D.

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.

The overloads of this function with the contactFilter parameter, can filter the returned results by the options in ContactFilter2D.

See Also: ContactFilter2D and RaycastHit2D.

Did you find this page useful? Please give it a rating: