Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

finding closest point on navmesh

Discussion in 'Navigation' started by Makari, Dec 9, 2014.

  1. Makari

    Makari

    Joined:
    Oct 19, 2013
    Posts:
    13
    I have a random wander script, it works fine as a prototype / testing so long as its on a flat surface and no obstacles, but now I want to clean it up some and have it use the navmesh

    so this random wander script waits a random amount of time and then generates a point (vector3)

    how can I then take that vector3 and find the closest point on navmesh? I've seen bits and pieces of like a navmesh.sample and some navmeshhit but I'm completely out of my element with these, so can someobody point me in the right direction? how would I take this random Vector3 point and get out a vector3 that is a point on the navmesh?
     
  2. tchris

    tchris

    Joined:
    Oct 10, 2012
    Posts:
    114
    Here is some code that is similar to what I am using. Hope this helps:
    Code (CSharp):
    1. public float distanceToNewPoint = 25f; //how far away is the new point
    2. NavMeshPath path = new NavMeshPath();
    3. Vector3 newDest;
    4. do {
    5.     //random vector3
    6.     Vector3 randomDirection = new Vector3 (Random.value, Random.value, Random.value);
    7.     //set the magnitude of the vector3 (this could be randomized too if you want)
    8.     randomDirection *= distanceToNewPoint;
    9.     //randomly pick a negative value for the x or z
    10.     if (Random.Range(0,2) == 0) randomDirection.x *= -1;
    11.     if (Random.Range(0,2) == 0) randomDirection.z *= -1;
    12.    
    13.     //add the random vector to the current position
    14.     Vector3 v = transform.position + randomDirection;
    15.    
    16.     //determine the y value by looking at the terrain height
    17.     float y;
    18.     y = Terrain.activeTerrain.SampleHeight (v) + Terrain.activeTerrain.transform.position.y;
    19.  
    20.     newDest = new Vector3 (v.x, y, v.z);
    21.    
    22.     //calculate the path
    23.     agent.CalculatePath(newDest, path);
    24.     Debug.Log(path.status);
    25.     yield return null;
    26. } while (path.status != NavMeshPathStatus.PathComplete); //keep looking for a new random value until the path is complete meaning it's a valid path
     
  3. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    353
    Is this what you are looking for?
    http://docs.unity3d.com/ScriptReference/NavMesh.SamplePosition.html

    If so, it's simple to use;

    Code (CSharp):
    1.  
    2. NavMeshHit myNavHit;
    3. if(NavMesh.SamplePosition(transform.position, out myNavHit, 100 , -1))
    4. {
    5. transform.position = myNavHit.position;
    6. }
    7.  
    That should snap the object onto the navmesh as long as it's within a range of 100 units.
     
    seanybaby2 likes this.
  4. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    875
    not sure understand why somethink's like that's does't works
    if (canWalk) {

    Vector3 vv = transform.position;
    vv += moveDirection * Time.fixedDeltaTime;


    NavMeshHit myNavHit;
    if (NavMesh.SamplePosition (vv,out myNavHit,2,-1)){

    collisionFlags = m_Controller.Move (moveDirection * Time.fixedDeltaTime);

    }


    }
     
  5. JosephNicholas22

    JosephNicholas22

    Joined:
    Oct 17, 2017
    Posts:
    1
    Saved my RTS. Thank you, my dude.
     
  6. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    2,181
    It was 4/5 years ago so unsure if they will even get your message :p