Search Unity

Resolved Help with NavMesh agent AI

Discussion in 'Navigation' started by Meaty114, Aug 28, 2022.

  1. Meaty114

    Meaty114

    Joined:
    Jul 25, 2022
    Posts:
    2
    Hello,

    I need some help with the built-in Unity ai system (navmeshes). I have a game with a rover that can be upgraded. By default, the agent component's avoidance quality is set to low. I want one upgrade that increases this to high, making the rover more capable of navigating tight spaces and avoiding other rovers. Ideally, this would also reduce the collision detection around the rover (specifically the agent component's box). However, it seems most tutorials are obsolete in terms of syntax, and I have no idea how the quality of the ai can be referenced from a script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoverUpgrades : MonoBehaviour
    6. {
    7.     public enum upgrade {none, cpu, wheelsHeavy, flashlight, roboArm};
    8.  
    9.     public upgrade currentUpgrade;
    10.  
    11.     void Update()
    12.     {
    13.         if (currentUpgrade == upgrade.cpu)
    14.         {
    15.             // The line below v should change the quality of the ai
    16.             GetComponent<UnityEngine.AI.NavMeshAgent>().ObstacleAvoidance.Type = 3;
    17.         }
    18.     }
    19.  
    20. }
    21.  
    -Unity version 2020.3.17f1
    -Processor: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz 2.20 GHz
    -RAM 16GB
    -Windows 10
     
    Last edited: Aug 28, 2022
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    You have to use the Enum values, the possible types are as following:


    Code (CSharp):
    1.   public enum ObstacleAvoidanceType
    2.   {
    3.     /// <summary>
    4.     ///   <para>Disable avoidance.</para>
    5.     /// </summary>
    6.     NoObstacleAvoidance,
    7.     /// <summary>
    8.     ///   <para>Enable simple avoidance. Low performance impact.</para>
    9.     /// </summary>
    10.     LowQualityObstacleAvoidance,
    11.     /// <summary>
    12.     ///   <para>Medium avoidance. Medium performance impact.</para>
    13.     /// </summary>
    14.     MedQualityObstacleAvoidance,
    15.     /// <summary>
    16.     ///   <para>Good avoidance. High performance impact.</para>
    17.     /// </summary>
    18.     GoodQualityObstacleAvoidance,
    19.     /// <summary>
    20.     ///   <para>Enable highest precision. Highest performance impact.</para>
    21.     /// </summary>
    22.     HighQualityObstacleAvoidance,
    23.   }
    24. }
    You can use them as for example:

    Code (CSharp):
    1.             GetComponent<NavMeshAgent>().obstacleAvoidanceType = ObstacleAvoidanceType.NoObstacleAvoidance;
     
    Meaty114 likes this.
  3. Meaty114

    Meaty114

    Joined:
    Jul 25, 2022
    Posts:
    2

    OMG thank you soo much it worked!