Skip to main content

NavMesh.Raycast(Vector3 sourcePosition, Vector3 targetPosition, NavMeshQueryFilter filter)

Traces a line between two positions on the NavMesh, subject to the constraints defined by the filter argument. The line is terminated on outer edges or a non-passable area.

Parameters:

NameTypeDescription
sourcePositionVector3The origin of the ray.
targetPositionVector3The end of the ray.
filterNavMeshQueryFilterA filter specifying which NavMesh areas can be passed when tracing the ray.

Returns:

typeDescription
booleanTrue if the ray is terminated before reaching target position. Otherwise returns false.
NavMeshHitproperties of the ray cast resulting location.

Usage

---@type Vector3
local sourcePosition;

---@type Vector3
local targetPosition;

---@type NavMeshQueryFilter
local filter;


local val0, val1 = NavMesh:Raycast(sourcePosition, targetPosition, filter)

ndary, the function returns true and the hit data is filled. If the path from the source to target is unobstructed, the function returns false. If the raycast terminates on an outer edge, hit.mask is 0; otherwise it contains the area mask of the blocking polygon. This function can be used to check if an agent can walk unobstructed between two points on the NavMesh. For example if you character has an evasive dodge move which needs space, you can shoot a ray from the characters location to multiple directions to find a spot where the character can dodge to.