Search Unity

How do I move pool generated nav mesh agents?

Discussion in 'Navigation' started by MarkSharpe, Dec 15, 2021.

  1. MarkSharpe

    MarkSharpe

    Joined:
    Feb 3, 2021
    Posts:
    27
    I have a bunch of nav mesh agents spawning from different pools across my scene but I need them to reference waypoints to move but of course my nav mesh agent prefab dumps the waypoint or destination goal even though its set to public in the movement script that's tied to it at run time. How do I have the nav mesh agent refence the goal position at start now that it is a pooled object spawn? This is from where the pooled nav agent dumped the public waypoints from the movement script. The object pooler is working as intended. UnassignedReferenceException: The variable wps of Wander has not been assigned. You probably need to assign the wps variable of the Wander script in the inspector. Wander.Update () (at Assets/Scripts/Wander.cs:25) (edited)

    void Start()
    {
    agent = this.GetComponent<NavMeshAgent>();
    int d = Random.Range(0, wps.Length);
    agent.SetDestination(wps[d].transform.position);
    }
    // Update is called once per frame
    void Update()
    {
    if(agent.remainingDistance < 2.5)
    {
    int d = Random.Range(0, wps.Length);
    agent.SetDestination(wps[d].transform.position);
    }
    else if (agent.hasPath)
    {
    Vector3 toTarget = agent.steeringTarget - this.transform.position;
    float turnAngle = Vector3.Angle(this.transform.forward, toTarget);
    agent.acceleration = turnAngle * agent.speed;
    }

    }
     
  2. steven1022

    steven1022

    Joined:
    Sep 8, 2012
    Posts:
    21
    Hard to follow your issue. Either save the path or partial path you need in a variable (NavMeshPath) and use it again or reset your enemy & recalculate a new path in OnEnable and use it instead.
     
  3. VeryBadPenny

    VeryBadPenny

    Joined:
    Jun 3, 2018
    Posts:
    40
    Did you solve this problem? From the look of it, it's not really a navmesh problem but the problem is that your wps - I assume waypoints - needs to be assigned a list of gameobjects. Looking at the error you might need to initialise your pooled object first with a list of gameobjects.