Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
ClosesourcePosition | The origin of the ray. |
targetPosition | The end of the ray. |
hit | Holds the properties of the ray cast resulting location. |
areaMask | A bitfield mask specifying which NavMesh areas can be passed when tracing the ray. |
bool True if the ray is terminated before reaching target position. Otherwise returns false.
Trace a line between two points on the NavMesh.
The source and destination points are first mapped on the NavMesh, then a ray is traced from the source point towards the target. If the ray hits a NavMesh boundary, 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.
The Raycast is different from physics ray cast because it works on “2.5D”, on the NavMesh. The difference to physics ray casts is that NavMesh ray casts can detect all kinds of navigation obstructions, such as holes in the ground, and it can also climb up slopes, if the area is navigable.
// TargetReachable using UnityEngine; using UnityEngine.AI;
public class TargetReachable : MonoBehaviour { public Transform target; private NavMeshHit hit; private bool blocked = false;
void Update() { blocked = NavMesh.Raycast(transform.position, target.position, out hit, NavMesh.AllAreas); Debug.DrawLine(transform.position, target.position, blocked ? Color.red : Color.green);
if (blocked) Debug.DrawRay(hit.position, Vector3.up, Color.red); } }
If you want to find the nearest point on the NavMesh, use physics ray cast to find a point in the world. For more information, refer to the Move to Click Point example.
sourcePosition | The origin of the ray. |
targetPosition | The end of the ray. |
hit | Holds the properties of the ray cast resulting location. |
filter | A filter specifying which NavMesh areas can be passed when tracing the ray. |
bool True if the ray is terminated before reaching target position. Otherwise returns false.
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.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.