Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

NavMeshHit

struct in UnityEngine.AI

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

Description

Information about a position that is the result of a query ran on the NavMesh.

The object represents a valid result if the distance and position 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.

Note: You can use float.isFinite() to determine whether a value is finite.

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);
        }
    }
}

Properties

Property Description
distanceDistance to the point of hit.
hitFlag set when the query encounters a particular valid situation.
maskBitmask that specifies the NavMesh area type at the point of hit.
normalNormal of the polygon edge where the query terminates.
positionPosition of hit.