Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question NavmeshPath - HowToDetectLinks

Discussion in 'Navigation' started by qrussard, Feb 24, 2023.

  1. qrussard

    qrussard

    Joined:
    Jun 15, 2021
    Posts:
    1
    hi, i'm struggling with detecting navmesh links as my custom navmesh agent.

    I have some hash table of manually placed navmeshlinks and i would like to know when agent is entering some of them. I observed using auto generated dropofflinks is not possible. The path is being calculated and none of the corners is exacly the same vector3 as world position of navmeshlink startPosition or endPosition when bidirectional mode. And it is simplier use case without setting width of navmesh link. After setting width larger than 0 is seems to be even more complicated.

    Have you ever been in point like that and would like to redirect me into some other aproach ?
     
  2. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    One approach you can try is to use triggers and colliders to detect when the agent enters a navmesh link. Here are the steps:

    1. Add a trigger collider to each navmesh link. You can do this manually or via script.

    2. Add a script to each navmesh link that listens for trigger events (OnTriggerEnter, OnTriggerExit, etc.).

    3. In the trigger event handler, check if the object that entered the trigger is your custom navmesh agent. If it is, you can then perform any necessary actions, such as triggering a pathfinding update.
    Here's an example of how you could implement this in code:

    Code (CSharp):
    1. public class NavmeshLinkTrigger : MonoBehaviour
    2. {
    3.    private void OnTriggerEnter(Collider other)
    4.    {
    5.        NavmeshAgentCustom agent = other.GetComponent<NavmeshAgentCustom>();
    6.        if (agent != null)
    7.        {
    8.            // Dump your code here
    9.        }
    10.    }
    11. }
    Note that you will need to set up the colliders and trigger events for each navmesh link manually, which could be time-consuming if you have a lot of them. However, this approach should be more reliable than trying to detect when the agent's path intersects with a navmesh link, since pathfinding can be imprecise and the agent's path might not always intersect with the link exactly.