Skip to content

Api > NavMeshAgent > Raycast()


NavMeshAgent.Raycast(Vector3 targetPosition)⚓︎

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

Parameters:⚓︎

Name Type Description
targetPosition Vector3 The desired end position of movement.

Returns:⚓︎

type Description
boolean True if there is an obstacle between the agent and the target position, otherwise false.
NavMeshHit Properties of the obstacle detected by the ray (if any, nil otherwise).

Usage⚓︎

---@type NavMeshAgent
local navmeshagent;

---@type Vector3
local targetPosition;


local val0, val1 = navmeshagent.Raycast(targetPosition)

Extra Detail⚓︎

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.