Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Been having trouble with navmesh for awhile now

Discussion in 'Scripting' started by Robster95, Nov 13, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    90
    I'm working on a project that has a few navmeshagents. One that constantly follows the player and other ones that patrol an area then follow the player if he gets to close.

    I am having a problem where if the player is too far away when the main following enemy spawns then the main enemy doesnt find a path and wont follow the player. I also have a lot of obstacles in the map (ex. trees houses, slopes) and whenever the enemy has to recalculate a path it stops then finds the path THEN follows the player. I've been reading a lot of documentation and tutorials on navmesh but I cant seem to figure out how to make the enemy follow the player and recalculate its path (which will be almost constant) then constantly walk towards the player with a smooth transition between paths.

    I am currently using setdestination(player.transform.position) and read that making a new path and using setpath may be better but read it will still make the agent stop and recalculate the path then start following again.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    11,133
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    90
    Ok then I've looked at this but then I still have another problem.

    If my player is to far away from the enemy with the navmesh attached to it and it wont find a path.

    Also how will I be able to store a constantly changing path?

    Would I do something like

    if(player.transform.postion != destination.position)
    {
    navmeshpath path = new navmeshpath;
    navmesh.calculatepath(player.transform.postion, path);
    }

    and then set the path? because if thats the way then I thought the constant checking and recalculating would end up being to much for computers.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    11,133
    I imagine one way might be to schedule repathings only every so often. Two heuristics off the top of my head:

    1. do it only every 5 seconds?

    2. do it only once the distance the player has moved (or the enemy has moved) is greater than 10 units.

    3. other ideas?

    There's also a method to find if the current path is stale, but that might be expensive too. The only way to really get what you want is to try combinations and see what happens, understand what the result is by printing things such as when the path is complete, when it is invalid, etc.
     
    Robster95 likes this.
  5. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    90
    Ok i'll try that! I also havent used coroutine's to wait a few seconds. Do you recommend doing that to check how much time has passed in order to calculate a new path?
     
  6. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    90
    Ok I was able to get this to work without the time check. Thank you so much for this help. But now I am running into a problem where the agent is walking towards a path that has a slope greater than it can walk on. I tried putting boxes around the whole cliff and giving them navmesh obstacles and carving them into the scene but the agent still walks towards the cliff and stops at the edge. I cant seem to figure out how to make it walk around the mountain instead of up it and towards the cliff
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    11,133
    Look at the navmesh in the editor window... Does it let you walk there? If so, fix that.
     
    Robster95 likes this.
  8. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    90
    Sorry for the late reply. But yes It allows me to walk there because the ground is all one giant terrain with different slopes. The cliffs angle is greater than the max walking slope so it isnt part of the navmesh and therefore not walkable even though it is still baked onto a walkable terrain.
     
unityunity