Search Unity

[SOLVED]Curvy unwanted path AND NavMeshAgent trying to reach unwalkable areas.

Discussion in 'Navigation' started by zazoum, Jun 9, 2015.

  1. zazoum

    zazoum

    Joined:
    Sep 3, 2011
    Posts:
    78
    Hello.
    I was using so far A* free, but I decided to switch to unity's pathfinding after 5 release.
    Im making a point n' click movement game. This is my script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class move : MonoBehaviour
    5. {
    6.     NavMeshAgent agent;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         agent = GetComponent<NavMeshAgent> ();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if (Input.GetMouseButtonUp (0)) {
    18.             RaycastHit hit;
    19.            
    20.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    21.             if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
    22.                 if (hit.collider != null) {
    23.                     if (hit.collider.tag == "edafos") {
    24.  
    25.                         agent.SetDestination (hit.point);
    26. //                  
    27.                     }
    28.                
    29.                 }
    30.            
    31.             }
    32.         }
    33.  
    34.     }
    35. }

    I have 2 problems:


    1) NavMeshAgent doesn't follow the shortest straight path, but a curvy path before it reaches final position where I click with mouse. I use the default setting in the inspector for the component.

    2) Let's say I have a big box and I want to make only the top side walkable. But when I click the other sides for example the NavMeshAgent tries to reach them. Isn't there a way to check if a Vector3 is part of a walkable area or not?
     
  2. MysterySoftware

    MysterySoftware

    Joined:
    Sep 18, 2014
    Posts:
    46
    1) Your agent should be following the shortest path possible (or easiest to reach), which doesn't necessarily have to be a straight line.

    2) To check whether or not a position is reachable you can use something like this
    Code (CSharp):
    1. NavMeshPath navpath = new NavMeshPath();
    2. NavMesh.CalculatePath (somePosition, someOtherPosition, -1, navpath);
    3. if (navpath.status == NavMeshPathStatus.PathPartial || navpath.status == NavMeshPathStatus.PathInvalid) {
    4.     // Not reachable
    5. }
    3) You shouldn't send the agent directly to the position the user clicked. You should sample the closest position on the NavMesh first, so you won't have to check whether or not the point is on the NavMesh. Your final code should look like something along those lines:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class move : MonoBehaviour {
    4.     NavMeshAgent agent;
    5.     void Awake () {
    6.         // Use GetGomponent in the Awake method rather than on Start
    7.         agent = GetComponent<NavMeshAgent> ();
    8.     }
    9.  
    10.     void Update () {
    11.         if (Input.GetMouseButtonUp (0)) {
    12.             RaycastHit hit;
    13.          
    14.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    15.             if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
    16.                 if (hit.collider != null) {
    17.                     if (hit.collider.tag == "edafos") {
    18.                    
    19.                         NavMeshHit navHit;
    20.                         NavMesh.SamplePosition (hit.point, out navHit, 1, -1);
    21.                        
    22.                         NavMeshPath navpath = new NavMeshPath();
    23.                         NavMesh.CalculatePath (somePosition, someOtherPosition, -1, navpath);
    24.                         if (navpath.status == NavMeshPathStatus.PathComplete) {
    25.                             agent.SetDestination (navHit.position);
    26.                         }            
    27.                     }
    28.              
    29.                 }
    30.          
    31.             }
    32.         }
    33.     }
    34. }
     
    Last edited: Jun 9, 2015
    zazoum likes this.
  3. zazoum

    zazoum

    Joined:
    Sep 3, 2011
    Posts:
    78
    Thanks for the example. It solved problem (2)!
    Now for the problem (1) and the curvy path I've made a small demo to show the issue:




    anybody has an idea of what's going on that produces that behavior?
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    zazoum likes this.
  5. zazoum

    zazoum

    Joined:
    Sep 3, 2011
    Posts:
    78
    Dustin-Horne likes this.