Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


GetRayIntersection

Cast a 3D ray against the 2D Colliders in the Scene.
Read time 2 minutesLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.Physics2DModule

GetRayIntersection(Ray, float, int)

Cast a 3D ray against the 2D Colliders in the Scene.
public static RaycastHit2D GetRayIntersection(Ray ray, float distance, int layerMask = -5)

Parameters

ray

The 3D ray defining origin and direction to test.

distance

The maximum distance over which to cast the ray.

layerMask

The LayerMask filter used to select which layers to detect Colliders for.

Returns

Type

Description

RaycastHit2DThe 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.
public static int GetRayIntersection(Ray ray, float distance, List<RaycastHit2D> results, int layerMask = -5)

Parameters

ray

The 3D ray defining origin and direction to test.

distance

The maximum distance over which to cast the ray.

The list to receive the results.

layerMask

The LayerMask filter used to select which layers to detect Colliders for.

Returns

Type

Description

intThe 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.
public static NativeArray<RaycastHit2D> GetRayIntersection(Ray ray, float distance, int layerMask = -5, Allocator allocator = Allocator.Temp)

Parameters

ray

The 3D ray defining origin and direction to test.

distance

The maximum distance over which to cast the ray.

layerMask

The LayerMask filter used to select which layers to detect Colliders for.

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>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.