Raycast
Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.
Read time 5 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.Physics2DModule
Raycast(Vector2, RaycastHit2D[], float, int, float, float)
Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.
public int Raycast(Vector2 direction, RaycastHit2D[] results, float distance, int layerMask, float minDepth, float maxDepth)
Parameters
Vector representing the direction of the ray.
Array to receive results.
Maximum distance over which to cast the ray.
Filter to check objects only on specific layers.
Only include objects with a Z coordinate (depth) greater than this value.
Only include objects with a Z coordinate (depth) less than this value.
Returns
Type | Description |
|---|---|
| int | The number of results returned. |
Remarks
The integer return value is the number of objects that intersect the ray (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 raycasts are performed frequently.
Overloads of this function that use filters the results by the options available in ContactFilter2D.
contactFilterAdditionally, this will also detect other Collider(s) at the start of the ray. In this case the ray 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 ray 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, RaycastHit2D, Physics2D.AllLayers, Physics2D.IgnoreRaycastLayer, _queriesHitTriggers
Raycast(Vector2, ContactFilter2D, RaycastHit2D[], float)
Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.
public int Raycast(Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance)
Parameters
Vector representing the direction of the ray.
Filter results defined by the contact filter.
Array to receive results.
Maximum distance over which to cast the ray.
Returns
Type | Description |
|---|---|
| int | The number of results returned. |
Remarks
The integer return value is the number of objects that intersect the ray (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 raycasts are performed frequently.
Overloads of this function that use filters the results by the options available in ContactFilter2D.
contactFilterAdditionally, this will also detect other Collider(s) at the start of the ray. In this case the ray 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 ray 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, RaycastHit2D, Physics2D.AllLayers, Physics2D.IgnoreRaycastLayer, _queriesHitTriggers
Raycast(Vector2, ContactFilter2D, List<RaycastHit2D>, float)
Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.
public int Raycast(Vector2 direction, ContactFilter2D contactFilter, List<RaycastHit2D> results, float distance = Infinity)
Parameters
Vector representing the direction of the ray.
Filter results defined by the contact filter.
List to receive results.
Maximum distance over which to cast the ray.
Returns
Type | Description |
|---|---|
| int | The number of results returned. |
Remarks
The integer return value is the number of Colliders that intersect the ray (possibly zero). 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 list does not need to be resized, and improves garbage collection performance when the query is performed frequently.
resultsAdditionally, this will also detect other Collider(s) at the start of the ray. In this case the ray 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 ray 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: RaycastHit2D
Raycast(Vector2, ContactFilter2D, float, Allocator)
Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.
public NativeArray<RaycastHit2D> Raycast(Vector2 direction, ContactFilter2D contactFilter, float distance = Infinity, Allocator allocator = Allocator.Temp)
Parameters
Vector representing the direction of the ray.
Filter results defined by the contact filter.
Maximum distance over which to cast the ray.
The memory allocator to use for the results. This can only be Allocator.Temp, Allocator.TempJob or Allocator.Persistent.
Returns
Type | Description |
|---|---|
| NativeArray<RaycastHit2D> | The NativeArray that contains the results. This must be disposed of unless it contains no results. |
Remarks
Additionally, this will also detect other Collider(s) at the start of the ray. In this case the ray 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 ray 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: RaycastHit2D