# 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 static bool Raycast(Vector3 origin, Vector3 direction, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### 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) | Returns true if the ray intersects with a Collider, otherwise false. |

### Remarks

To select which layers a ray should collide with, use a [LayerMask](/engine/6000.5/script-reference/unityengine/layermask.md).

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.

**Notes:** Raycasts will not detect Colliders for which the Raycast origin is inside the Collider. In all these examples [FixedUpdate](/engine/6000.5/script-reference/unityengine/playerloop/fixedupdate.md) is used rather than [Update](/engine/6000.5/script-reference/unityengine/playerloop/update.md). Refer to [Order of execution for event functions](/engine/6000.5/manual/scripting/managing-update-order/execution-order.md) to understand the difference between [Update](/engine/6000.5/script-reference/unityengine/playerloop/update.md) and [FixedUpdate](/engine/6000.5/script-reference/unityengine/playerloop/fixedupdate.md), and to see how they relate to physics queries.

```csharp
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    LayerMask layerMask;

    void Awake()
    {
        layerMask = LayerMask.GetMask("Wall", "Character");
    }

    // See https://docs.unity.com/engine/6000.5/manual/scripting/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries
    void FixedUpdate()
    {

        RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))

        {
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
            Debug.Log("Did Hit");
        }
        else
        {
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
            Debug.Log("Did not Hit");
        }

    }
}
```

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

```csharp
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    // See https://docs.unity.com/engine/6000.5/manual/scripting/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries
    void FixedUpdate()
    {
        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        if (Physics.Raycast(transform.position, fwd, 10))
            print("There is something in front of the object!");
    }
}
```

## Raycast(Vector3, Vector3, RaycastHit, float, int, QueryTriggerInteraction)

Casts a ray against all colliders in the Scene and returns detailed information on what was hit.

```csharp
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### 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 closest 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) | Returns true when the ray intersects any collider, otherwise false. |

### Remarks

This example reports the distance between the current object and the reported Collider:

```csharp
using UnityEngine;

public class RaycastExample : MonoBehaviour
{
    // See https://docs.unity.com/engine/6000.5/manual/scripting/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries
    void FixedUpdate()
    {
        RaycastHit hit;

        if (Physics.Raycast(transform.position, -Vector3.up, out hit))
            print("Found an object - distance: " + hit.distance);
    }
}
```

This example re-introduces the `maxDistance` parameter to limit how far ahead to cast the Ray:

```csharp
using UnityEngine;

public class RaycastExample : MonoBehaviour
{
    // See https://docs.unity.com/engine/6000.5/manual/scripting/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries
    void FixedUpdate()
    {
        RaycastHit hit;

        if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0f))
            print("Found an object - distance: " + hit.distance);
    }
}
```

## Raycast(Ray, float, int, QueryTriggerInteraction)

Same as above using /ray.origin/ and /ray.direction/ instead of `origin` and `direction`.

```csharp
public static bool Raycast(Ray ray, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### Parameters

**** (\[Ray]\(/engine/6000.5/script-reference/unityengine/ray)): The starting point and 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) | Returns true when the ray intersects any collider, otherwise false. |

### Examples

```csharp
using UnityEngine;
using UnityEngine.InputSystem;

public class ExampleClass : MonoBehaviour
{
    // See https://docs.unity.com/engine/6000.5/manual/scripting/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries
    void FixedUpdate()
    {
        Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
        if (Physics.Raycast(ray, 100))
            print("Hit something!");
    }
}
```

## Raycast(Ray, RaycastHit, float, int, QueryTriggerInteraction)

Same as above using /ray.origin/ and /ray.direction/ instead of `origin` and `direction`.

```csharp
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### Parameters

**** (\[Ray]\(/engine/6000.5/script-reference/unityengine/ray)): The starting point and direction of the ray.**** (\[RaycastHit]\(/engine/6000.5/script-reference/unityengine/raycasthit)): If true is returned, `hitInfo` will contain more information about where the closest 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) | Returns true when the ray intersects any collider, otherwise false. |

### Remarks

This example draws a line along the length of the Ray whenever a collision is detected:

### Examples

```csharp
using UnityEngine;
using UnityEngine.InputSystem;

public class ExampleClass : MonoBehaviour
{
    // See https://docs.unity.com/engine/6000.5/manual/scripting/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries
    void FixedUpdate()
    {
        Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
            Debug.DrawLine(ray.origin, hit.point);
    }
}
```
