NavMeshHit
Information about a position that is the result of a query ran on the NavMesh.
Read time 1 minuteLast updated 9 days ago
Definition
- Type: Struct
- Namespace: UnityEngine.AI
- Assembly: UnityEngine.AIModule
public struct NavMeshHit
Remarks
The object represents a valid result if the and properties have finite values. Otherwise, the object represents a result that could not be calculated from the input data provided to the query. Refer to the documentation of each query method for details about the situations that produce invalid results.
distancepositionNote: You can use to determine whether a value is finite.
float.isFinite()Additional Resources: NavMesh.SamplePosition, NavMesh.FindClosestEdge, NavMesh.Raycast, NavMeshAgent.FindClosestEdge, NavMeshAgent.Raycast, NavMeshAgent.SamplePathPosition
Examples
using UnityEngine;using UnityEngine.AI;public class ShowLineOfSightToTarget : MonoBehaviour{ public Transform target; NavMeshHit m_RayEnd; void Update() { // Detect whether a NavMesh edge blocks a straight path to the target. NavMesh.Raycast(transform.position, target.position, out m_RayEnd, NavMesh.AllAreas); var isRaySegmentOnNavMesh = float.IsFinite(m_RayEnd.distance); if (isRaySegmentOnNavMesh) { Debug.DrawLine(transform.position, m_RayEnd.position, m_RayEnd.hit ? Color.red : Color.green); Debug.DrawLine(m_RayEnd.position, target.position, Color.gray); if (m_RayEnd.hit) { Debug.DrawLine(m_RayEnd.position, m_RayEnd.position + Vector3.up, Color.red); Debug.DrawLine(m_RayEnd.position, m_RayEnd.position + m_RayEnd.normal, Color.yellow); } } else { Debug.DrawLine(transform.position, target.position, Color.magenta); } }}