Unity - Scripting API: Physics.Raycast
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.
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.
Declaration
public static bool Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
Parameters
| Parameter | Description |
|---|---|
| origin | The starting point of the ray in world coordinates. |
| direction | The direction of the ray. |
| maxDistance | The max distance the ray should check for collisions. |
| layerMask | A Layer mask that is used to selectively filter which colliders are considered when casting a ray. |
| queryTriggerInteraction | Specifies whether this query should hit Triggers. |
Returns
bool Returns true if the ray intersects with a Collider, otherwise false.
Description
Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.
To select which layers a ray should collide with, use a LayerMask.
Specifying queryTriggerInteraction allows you to control whether or not Trigger colliders generate a hit, or whether to use the global Physics.queriesHitTriggers setting.
Notes: Raycasts will not detect Colliders for which the Raycast origin is inside the Collider. In all these examples FixedUpdate is used rather than Update. Refer to Order of execution for event functions to understand the difference between Update and FixedUpdate, and to see how they relate to physics queries.
This example creates a simple Raycast, projecting forwards from the position of the object's current position, extending for 10 units.
Parameters
| Parameter | Description |
|---|---|
| origin | The starting point of the ray in world coordinates. |
| direction | The direction of the ray. |
| hitInfo | If true is returned, hitInfo will contain more information about where the closest collider was hit. (Additional resources: RaycastHit). |
| maxDistance | The max distance the ray should check for collisions. |
| layerMask | A Layer mask that is used to selectively filter which colliders are considered when casting a ray. |
| queryTriggerInteraction | Specifies whether this query should hit Triggers. |
Returns
bool Returns true when the ray intersects any collider, otherwise false.
Description
Casts a ray against all colliders in the Scene and returns detailed information on what was hit.
This example reports the distance between the current object and the reported Collider:
This example re-introduces the maxDistance parameter to limit how far ahead to cast the Ray:
Declaration
public static bool Raycast(Ray ray, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
Parameters
| Parameter | Description |
|---|---|
| ray | The starting point and direction of the ray. |
| maxDistance | The max distance the ray should check for collisions. |
| layerMask | A Layer mask that is used to selectively filter which colliders are considered when casting a ray. |
| queryTriggerInteraction | Specifies whether this query should hit Triggers. |
Returns
bool Returns true when the ray intersects any collider, otherwise false.
Description
Same as above using ray.origin and ray.direction instead of origin and direction.
Declaration
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
Parameters
| Parameter | Description |
|---|---|
| ray | The starting point and direction of the ray. |
| hitInfo | If true is returned, hitInfo will contain more information about where the closest collider was hit. (Additional resources: RaycastHit). |
| maxDistance | The max distance the ray should check for collisions. |
| layerMask | A Layer mask that is used to selectively filter which colliders are considered when casting a ray. |
| queryTriggerInteraction | Specifies whether this query should hit Triggers. |
Returns
bool Returns true when the ray intersects any collider, otherwise false.
Description
Same as above using ray.origin and ray.direction instead of origin and direction.
This example draws a line along the length of the Ray whenever a collision is detected: