Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Navmesh agent is falling close to the floor/surface is there a way to keep the agent height ?

Discussion in 'Navigation' started by Chocolade, May 7, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    917
    My agent is a drone that is already floating in the air and should float in the air. but after adding the navemsh agent component to it when running the game the droid is falling close to the floor/surface and never keep his original height.

    This is a screenshot of the drone inspector settings start position height on Y is 51.71



    And this is a screenshot of the drone after running the game after he is falling/getting closer to the floor/surface:
    Now the height position on Y is 50.083



    I opened the navigation window : in the agents tab tried to change the height from 2 to 7 or more but it didn't work so I tried in the bake tab tried to change the agent height the step height the drop height but nothing helped he keep getting closer to the floor/surface.

    This is the script I'm using with the drone to move the drone between waypoints.
    The script is working fine but the drone height is too low. I want to be able to control the drone height :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7.  
    8. public class Waypoints : MonoBehaviour
    9. {
    10.     public List<Transform> points = new List<Transform>();
    11.     public List<GameObject> npcs;
    12.     public NavMeshAgent agent;
    13.  
    14.     private int destPoint = 0;
    15.     private int damping = 2;
    16.  
    17.     void Start()
    18.     {
    19.         var wayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
    20.         foreach (GameObject waypoint in wayPoints)
    21.         {
    22.             points.Add(waypoint.transform);
    23.         }
    24.  
    25.         npcs = GameObject.FindGameObjectsWithTag("Npc").ToList();
    26.  
    27.         // Disabling auto-braking allows for continuous movement
    28.         // between points (ie, the agent doesn't slow down as it
    29.         // approaches a destination point).
    30.         agent.autoBraking = false;
    31.  
    32.         GotoNextPoint();
    33.     }
    34.  
    35.  
    36.     void GotoNextPoint()
    37.     {
    38.         // Returns if no points have been set up
    39.         if (points.Count == 0)
    40.             return;
    41.  
    42.         // Set the agent to go to the currently selected destination.
    43.         agent.destination = points[destPoint].position;
    44.  
    45.         // Choose the next point in the array as the destination,
    46.         // cycling to the start if necessary.
    47.         destPoint = (destPoint + 1) % points.Count;
    48.     }
    49.  
    50.     void VisitNpcs()
    51.     {
    52.         var npc = npcs[Random.Range(0, npcs.Count)];
    53.         var distance = Vector3.Distance(npc.transform.position, agent.transform.position);
    54.  
    55.         if (distance > 7)
    56.         {
    57.             agent.destination = npc.transform.position;
    58.             var collider = agent.GetComponent<BoxCollider>();
    59.             if (collider != null)
    60.             {
    61.                 collider.enabled = false;
    62.             }
    63.         }
    64.  
    65.         if (distance < 2)
    66.         {
    67.             agent.isStopped = true;
    68.  
    69.             Vector3 lookPos = agent.transform.position - npc.transform.position;
    70.             lookPos.y = 0;
    71.             var rotation = Quaternion.LookRotation(lookPos);
    72.             npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, rotation, Time.deltaTime * damping);
    73.         }
    74.     }
    75.  
    76.     void Update()
    77.     {
    78.         // Choose the next destination point when the agent gets
    79.         // close to the current one.
    80.  
    81.         if (!agent.pathPending && agent.remainingDistance < 0.5f)
    82.         {
    83.             GotoNextPoint();
    84.         }
    85.  
    86.         VisitNpcs();
    87.     }
    88. }
    89.  
    I want to keep the drone in his original height.
     
    Last edited: May 8, 2020
  2. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    347
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    917
    Thank you.

    Working. I changed it before asking here but it didn't work.but now I changed it much more I moved the base offset much more down almost to ground level and now it's working. Maybe the mesh pivot position on my droid is wrong ?

    Anyway the solution is like you wrote changing the base offset.

    This is working for me :