- Home /
NavMesh Agent not updating (Setting) new path
Im using the NavMesh agent to move my "Inhabitants" around the scene. The desinations are set depending on what time of day it is. There are three destinations at present, a "Home" a "Work" and a "Hobbies" location.
Now the code is getting called, s the debugs and text is being called correctly. BUT the actual pathfinding isnt updating and they stay in the same place even when we move on to another part of the day.. Havny really used NavMesh pathfinding before, so might be simple. But as far as im aware calling the SetDestination method of the NavMesh Agent is all you need to do to update the path.
Heres the code.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// Now using unity built in nav mesh for pathfinding.
public class NavMeshPathfinding : MonoBehaviour {
public enum Destinations{
home,
work,
hobbies
};
Destinations destination;
// The actual destinations in the scene.
public GameObject home;
public GameObject work;
public GameObject hobbies;
public Text destinationText;
NavMeshAgent agent;
private GameObject GM;
private WorldTime worldTime;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
GM = GameObject.FindGameObjectWithTag(Tags.GM);
worldTime = GM.GetComponent<WorldTime>();
destinationText.text = "Idle";
}
// Update is called once per frame
void Update () {
// when its a weekday..
if (destination != Destinations.home && (worldTime.timeOfDay == WorldTime.TimeOfDay.Morning || worldTime.timeOfDay == WorldTime.TimeOfDay.Nightime) )
{
destination = Destinations.home;
GetNewDestination();
}
else if (worldTime.day == WorldTime.Days.Saturday || worldTime.day == WorldTime.Days.Sunday)
{
if (destination != Destinations.hobbies && worldTime.timeOfDay == WorldTime.TimeOfDay.WorkTime)
{
destination = Destinations.hobbies;
GetNewDestination();
}
}
else
{
if (destination != Destinations.work && worldTime.timeOfDay == WorldTime.TimeOfDay.WorkTime)
{
destination = Destinations.work;
GetNewDestination();
}
}
}
public void GetNewDestination ()
{
if (destination == Destinations.home)
{
Debug.Log("Heading to" + destination);
agent.SetDestination(home.transform.position);
destinationText.text = "Heading home";
}
else if (destination == Destinations.work)
{
Debug.Log( "Heading too "+ destination);
agent.SetDestination(work.transform.position);
destinationText.text = "Time for work";
}
else if (destination == Destinations.hobbies)
{
Debug.Log("Heading to: "+destination);
agent.SetDestination(hobbies.transform.position);
destinationText.text = "Now for some me time.";
}
}
}
So they head to the inital desination "home" and then stay there..
So what am i doing wrong troops?
Thanks in advance..
hmm, strange, I don't see anything wrong, and calling SetDestination should have agents recalculate their paths as far as I'm aware...it's late here and I probably shouldn't be trying to answer things on the forums half asleep but...have you tried calling RecalculatePath() after you change an agents destination?
hey bud. RecalculatePath needs a "Path" parameter, im pretty sure, and the destinations are Vectors. I dont know how i can make the destinations a path ins$$anonymous$$d of a Vector, or even if you can..
Answer by DanSuperGP · Mar 10, 2015 at 11:21 PM
In Unity 5, NavMesh Agents will not automatically resume when a new destination is set. You need to manually call Resume()
This is noted in the Unity 5 Upgrade Guide http://docs.unity3d.com/Manual/UpgradeGuide5-AI.html
Im using 4.6.1. SetDestination shoukd overwrite any existing paths as far as i know.. i dont get it. any other suggestions?
Your answer
Follow this Question
Related Questions
Possible to edit the movement of NavMeshAgent ? 2 Answers
Ignoring or overruling a NavMeshAgent destination 0 Answers
NavMesh Agent glitches in trees 3 Answers
Editing NavMesh Paths? 0 Answers
Horde of NavMeshAgents - stops to recalculate path. 4 Answers