# Raycast

> Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **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.

```csharp
public bool Raycast(Vector3 origin, Vector3 direction, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
```

### Parameters

**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The starting point of the ray in world coordinates.**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The direction of the ray.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The max distance the ray should check for collisions.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A Layer mask that is used to selectively filter which colliders are considered when casting a ray.**** (\[QueryTriggerInteraction]\(/engine/6000.5/script-reference/unityengine/querytriggerinteraction)): Specifies whether this query should hit Triggers.

### Returns

| Type                                                          | Description                                                  |
| ------------------------------------------------------------- | ------------------------------------------------------------ |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | True if the ray intersects with a Collider, otherwise false. |

### Remarks

You may optionally provide a [LayerMask](/engine/6000.5/script-reference/unityengine/layermask.md), 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](/engine/6000.5/script-reference/unityengine/physics/querieshittriggers.md) setting.

This example creates a simple Raycast, projecting forwards from the position of the object's current position, extending for 10 units.

### Examples

```csharp
using UnityEngine;

public class Example : 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);

        // Max ray distance
        float maxDistance = 10f;

        // RaycastAll in the current physics scene
        RaycastHit[] hits = new RaycastHit[10]; // Pre-allocate for performance
        int hitCount = physicsScene.Raycast(origin, direction, hits, maxDistance);

        if (hitCount > 0)
        {
            Debug.Log($"Detected {hitCount} hit(s) in front of the object:");
            for (int i = 0; i < hitCount; i++)
            {
                Debug.Log($"Hit {i}: {hits[i].collider.name} at {hits[i].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.

```csharp
public bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
```

### Parameters

**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The starting point of the ray in world coordinates.**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The direction of the ray.**** (\[RaycastHit]\(/engine/6000.5/script-reference/unityengine/raycasthit)): If true is returned, `hitInfo` will contain more information about where the collider was hit. (Additional Resources: [RaycastHit](/engine/6000.5/script-reference/unityengine/raycasthit.md)).**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The max distance the ray should check for collisions.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A Layer mask that is used to selectively filter which colliders are considered when casting a ray.**** (\[QueryTriggerInteraction]\(/engine/6000.5/script-reference/unityengine/querytriggerinteraction)): Specifies whether this query should hit Triggers.

### Returns

| Type                                                          | Description                                                  |
| ------------------------------------------------------------- | ------------------------------------------------------------ |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | True if the ray intersects with a Collider, otherwise false. |

### Remarks

This method generates no garbage.

### Examples

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

```csharp
public int Raycast(Vector3 origin, Vector3 direction, RaycastHit[] raycastHits, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
```

### Parameters

**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The starting point and direction of the ray.**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The direction of the ray.**** (\[RaycastHit\[]]\(/engine/6000.5/script-reference/unityengine/raycasthit)): The buffer to store the hits into.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The max distance the rayhit is allowed to be from the start of the ray.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A Layer mask that is used to selectively filter which colliders are considered when casting a ray.**** (\[QueryTriggerInteraction]\(/engine/6000.5/script-reference/unityengine/querytriggerinteraction)): The amount of hits stored into the `results` buffer.

### Returns

| Type                                                       | Description                                                  |
| ---------------------------------------------------------- | ------------------------------------------------------------ |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | True if the ray intersects with a Collider, otherwise false. |

### Remarks

This method generates no garbage.
