NavMeshAgent.Raycast

function Raycast (targetPosition : Vector3, out hit : NavMeshHit) : boolean

Parameters

NameDescription
targetPosition The desired end position of movement.
hit Holds the properties of the resulting unblocked position.

Returns

boolean - True If terminated before reaching target position. Otherwise returns false.

Description

Trace movement towards a target postion in the NavMesh. Without moving the agent.

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 because the line tracing is performed in a simpler way using the navmesh and has a lower processing overhead.

JavaScript
var target: Transform;
private var agent: NavMeshAgent;
function Start () {
agent = GetComponent.<NavMeshAgent>();
}
function Update() {
var hit: NavMeshHit;

// Note the negative test condition!
if (!agent.Raycast(target.position, hit)) {
// Target is "visible" from our position.
}
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public Transform target;
private NavMeshAgent agent;
void Start() {
agent = GetComponent<NavMeshAgent>();
}
void Update() {
NavMeshHit hit;
if (!agent.Raycast(target.position, out hit)) {
}
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public target as Transform

private agent as NavMeshAgent

def Start():
agent = GetComponent[of NavMeshAgent]()

def Update():
hit as NavMeshHit
if not agent.Raycast(target.position, hit):
pass