# Raycast(Vector3, out NavMeshHit)

> Trace a straight path towards a target postion in the NavMesh without moving the agent.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.AI](/engine/6000.3/script-reference/unityengine/ai.md)
* **Assembly:** UnityEngine.AIModule

## Raycast(Vector3, NavMeshHit)

```csharp
public bool Raycast(Vector3 targetPosition, out NavMeshHit hit)
```

### Parameters

**** (\[Vector3]\(/engine/6000.3/script-reference/unityengine/vector3)): The desired end position of movement.**** (\[NavMeshHit]\(/engine/6000.3/script-reference/unityengine/ai/navmeshhit)): Properties of the obstacle detected by the ray (if any).

### Returns

| Type                                                          | Description                                                                              |
| ------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | True if there is an obstacle between the agent and the target position, otherwise false. |

### Remarks

This function follows the path of a "ray" between the agent's position and the specified target position. If an obstruction is encountered along the line then a true value is returned and the position and other details of the obstructing object are stored in the `hit` parameter. This can be used to check if there is a clear shot or line of sight between a character and a target object. This function is preferable to the similar [Physics.Raycast](/engine/6000.3/script-reference/unityengine/physics/raycast.md) because the line tracing is performed in a simpler way using the navmesh and has a lower processing overhead.

### Examples

```csharp
using UnityEngine;
using UnityEngine.AI;

public class ExampleClass : MonoBehaviour
{
    public Transform target;
    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        NavMeshHit hit;
        if (!agent.Raycast(target.position, out hit))
        {
            // Target is "visible" from our position.
        }
    }
}
```
