Search Unity

Prevent movement to the nearest possible location

Discussion in 'Navigation' started by Abdou23, Mar 13, 2017.

  1. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    Screen Shot 2017-03-13 at 1.33.41 PM.png
    As you can see in the image above when I click on the white path the object move perfectly towards the clicked position. When I click on the blue ground the object doesn't move there but it finds the nearest possible location on the white path, which is the behavior that I don't want.
    I want the object to not move if the click is outside of the white path.

    White Path: Navigation Static - Walkable
    Blue Ground: Nothing.

    Code:
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (Input.GetButtonDown("Fire1"))
    4.         {
    5.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    6.             RaycastHit hit;
    7.             if (Physics.Raycast(ray, out hit, 100))
    8.             {
    9.                 navAgent.destination = hit.point;
    10.                 navAgent.Resume();
    11.             }
    12.         }  
    13.     }
     
  2. AndresSepulveda

    AndresSepulveda

    Joined:
    Mar 12, 2014
    Posts:
    52
    -if blue zone is clicked don't perform the navmesh sample
    -Calculate the path before giving it to the agent.
    - leave the blue area out of the navmesh build.
     
    dadude123 likes this.
  3. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    Should I do all three or one of them, also, would you elaborate more on how to calculate the path before giving it to the Agent and how would it be different from the code above.
    Plus as I mentioned the blue ground doesn't have any checked options in the Navigation window.
     
  4. AndresSepulveda

    AndresSepulveda

    Joined:
    Mar 12, 2014
    Posts:
    52
    In my game, when i click outside the navmesh (like the skybox) my char doesn't move so i believe that must double check that.

    If by any reason if still is trying to move over there, you can
    1- check when you click the blue area (if it is a mesh clickeable) skip the rest of the code. a return maybe.
    2- if that is not possible, you can use NavMesh.CalculatePath and check if the path status property