Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

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

Physics2D.LinecastNonAlloc

static function LinecastNonAlloc(start: Vector2, end: Vector2, results: RaycastHit2D[], layerMask: int = DefaultRaycastLayers, minDepth: float = -Mathf.Infinity, maxDepth: float = Mathf.Infinity): int;
static int LinecastNonAlloc(Vector2 start, Vector2 end, RaycastHit2D[] results, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
static def LinecastNonAlloc(start as Vector2, end as Vector2, results as RaycastHit2D[], layerMask as int = DefaultRaycastLayers, minDepth as float = -Mathf.Infinity, maxDepth as float = Mathf.Infinity) as int

Parameters

minDepthOnly include objects with a Z coordinate (depth) greater than this value.
maxDepthOnly include objects with a Z coordinate (depth) less than this value.
startThe start point of the line in world space.
endThe end point of the line in world space.
resultsReturned array of objects that intersect the line.
layerMaskFilter to detect Colliders only on certain layers.

Returns

int The number of results returned.

Description

Casts a line against colliders in the scene.

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

This function is similar to the LinecastAll function except that the results are returned in the supplied array. The integer return value is the number of objects that intersect the line (possibly zero) but the results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when linecasts are performed frequently. The line is assumed to run from its start point to its end point; colliders will be placed in the returned array in order of distance from the start of the line.

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, Linecast, LinecastAll, DefaultRaycastLayers, IgnoreRaycastLayer, raycastsHitTriggers.