Raycast
Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.
Read time 3 minutesLast updated 12 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.PhysicsModule
Raycast(Vector3, Vector3, float, int, QueryTriggerInteraction)
Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.
public bool Raycast(Vector3 origin, Vector3 direction, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
Parameters
The starting point of the ray in world coordinates.
The direction of the ray.
The max distance the ray should check for collisions.
A Layer mask that is used to selectively ignore Colliders when casting a ray.
Specifies whether this query should hit Triggers.
Returns
Type | Description |
|---|---|
| bool | True if the ray intersects with a Collider, otherwise false. |
Remarks
You may optionally provide a LayerMask, to filter out any Colliders you aren't interested in generating collisions with. Specifying queryTriggerInteraction allows you to control whether or not Trigger colliders generate a hit, or whether to use the global Physics.queriesHitTriggers setting.
This example creates a simple Raycast, projecting forwards from the position of the object's current position, extending for 10 units.
Examples
using UnityEngine;public class ExampleClass : MonoBehaviour{ void FixedUpdate() { // Get the current PhysicsScene PhysicsScene physicsScene = gameObject.scene.GetPhysicsScene(); // Define ray direction and origin Vector3 origin = transform.position; Vector3 direction = transform.TransformDirection(Vector3.forward); // Optional: set max distance and layerMask if desired float maxDistance = 10f; // Perform the raycast using the instance method if (physicsScene.Raycast(origin, direction, out RaycastHit hitInfo, maxDistance)) { Debug.Log("There is something in front of the object in the current physics scene!" + hitInfo.point); } }}
Raycast(Vector3, Vector3, RaycastHit, float, int, QueryTriggerInteraction)
Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.
public bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
Parameters
The starting point of the ray in world coordinates.
The direction of the ray.
If true is returned, hitInfo will contain more information about where the collider was hit. (Additional Resources: RaycastHit).
The max distance the ray should check for collisions.
A Layer mask that is used to selectively ignore Colliders when casting a ray.
Specifies whether this query should hit Triggers.
Returns
Type | Description |
|---|---|
| bool | True if the ray intersects with a Collider, otherwise false. |
Remarks
This method generates no garbage.
Examples
using UnityEngine;public class RaycastExample : MonoBehaviour{ void FixedUpdate() { RaycastHit hit; if (Physics.Raycast(transform.position, -Vector3.up, out hit)) print("Found an object - distance: " + hit.distance); }}
Raycast(Vector3, Vector3, RaycastHit[], float, int, QueryTriggerInteraction)
Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.
public int Raycast(Vector3 origin, Vector3 direction, RaycastHit[] raycastHits, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
Parameters
The starting point and direction of the ray.
The direction of the ray.
The buffer to store the hits into.
The max distance the rayhit is allowed to be from the start of the ray.
A Layer mask that is used to selectively ignore colliders when casting a ray.
The amount of hits stored into the results buffer.
Returns
Type | Description |
|---|---|
| int | True if the ray intersects with a Collider, otherwise false. |
Remarks
This method generates no garbage.