Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

i have a Z,X axis how can i get the Y on the navmesh surface

Discussion in 'Navigation' started by naorgueva, Oct 15, 2019.

  1. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    19
    Code (CSharp):
    1.     private void JumpAround()
    2.     {
    3.  
    4.         Vector3 pointInSphare = Random.insideUnitSphere * range;
    5.         pointInSphare = _thisTransform.TransformPoint(pointInSphare);
    6.         pointInSphare.y = 800;
    7.  
    8.         Ray ray = new Ray(pointInSphare, Vector3.down);
    9.         RaycastHit hit;
    10.         if(Physics.Raycast(ray ,out hit, _ww,  _inThisLayer))
    11.         {
    12.             pointInSphare = hit.point;
    13.             _thisNavMeshAgent.SetDestination(pointInSphare);
    14.         }
    15.     }
    im trading to make a game without physics so i cant use raycast.
    i want a frog to move randomly on the navmesh.
    correctly im casting a ray to determine the y of the floor.
    i have Z and X axis.
    can i get the correct Y on the navmesh surface?
     
    Last edited: Oct 15, 2019
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    381
    Instead of casting rays, you could use NavMesh.SamplePosition(pointInSphere) to get a point on the NavMesh.
    Just generate your random point there and immediately use SamplePosition, no need to first set it relative to thistransform or anything like that.
     
  3. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    19
    thank you for your reply
    how do i access the navmesh?
    when i write this
    [SerializeField] private NavMesh _navMesh;
    i have an error "cannot declare variable of static type"
     
  4. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    381
    Like it says in the error: NavMesh is a static class you can simply access from anywhere using "NavMesh.WantedMethod".
    You don't make NavMesh variables. Think of static classes like a singular object that exists somewhere in the game and can be easily accessed using simply the class name.
     
  5. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    19
    ok good
    you are so helpful!
    now im using the new Navmesh Surface
    and i have 5 different Navmesh Surfaces bakes
    how dose it knows witch one i to look for?
    that is why i wanted to "SerializeField"
     
  6. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    381
    Unfortunately Unity will always sample with the first NavMesh it holds in memory, there's no way to differentiate here. The NavMesh class was never rebuilt to account for multiple NavMeshes in a scene, which only became possible when they added NavMeshComponents.
     
  7. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    19
    so i have the new NavMesh scripts that came with the "NavMeshSurface"
    >>>>NavMeshLink
    >>>>NavMeshVolume
    >>>>NavMeshModifier
    anything can help me?

    correctly the Ground is under "Ground" layer,
    and the NavMesh.SamplePosition can detect it if i set the [serializefield] _inThisLayer. = "Ground" layer.
    witch is wired cos:
    1. The Ground is under "Ground" layer.
      AND
    2. i have nothing under "UI" layer.
     
    Last edited: Oct 17, 2019