- Home /
Issue with pathfinding after upgrade to Unity 5
I upgraded my project from Unity 4.6 and followed the steps for rebaking my navmesh. It is a simple quad navmesh with no obstacles used for nav mesh agents. My player object is not controlled with a Nav Agent but through update through the use of getting user Input. The problem is if I continuously move my player the enemies spawned in the scene through Instantiate appear to wait before beginning to pathfind to the the player even though I can see that their SetDestination is called. Even after they start moving, if player movement resumes heavily their velocity seems to slow and they appear to be waiting. I have the pathfinding use SetDestination from a Coroutine every 1/3 second or so, increasing or decreasing has not solved the issue. I also tried adding a call to navAgent.Resume() after the path is reset (I read in the docs that this has to manually be done in unity 5 but it didn't solve the problem). In unity 4.6, my enemies immediately began pathfinding to the player regardless of player movement. I have isolated the code below. I have also found that Nav Agents preloaded in the scene will pathfind normally, the problem is only occuring with the pathfinding prefabs that are instantiated into the scene during runtime. Has anybody else run into this issue?
public class PlayerController : MonoBehaviour {
public float speedx, speedz;
private void ComputerControls()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal * speedx, GetComponent<Rigidbody>().velocity.y, moveVertical * speedz);//using the current velocity for y
GetComponent<Rigidbody>().velocity = movement;
}
void Update () {
ComputerControls();
}
}
nav agent script
public class NavScript : MonoBehaviour {
private PlayerController player;
private NavMeshAgent nav;
// Use this for initialization
void Start () {
nav = GetComponent<NavMeshAgent>();
player = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
StartCoroutine (LookForPlayer ());//begin looking for player
}
protected virtual IEnumerator LookForPlayer(){
while(true){
nav.SetDestination (player.transform.position);
yield return new WaitForSeconds(0.3f);
}
}
}
Answer by lattman · May 18, 2015 at 11:22 PM
I may have discovered the issue. After much trial and error, I found that deselecting "auto breaking" on the NavMeshAgent allowed the prefab agent to immediately start pathfinding. I do not know why the instantiated prefab behaves differently than the prefab placed in the scene before run-time, but apparently there is some difference.
Your answer
Follow this Question
Related Questions
Unity New UI system not working after the project is upgraded 5 Answers
Two-dimensional array of game objects to create a graph of nodes? 0 Answers
Unity 5 upgrade broke lightmapped shader 1 Answer
Upgrading from 4 to 5 0 Answers
A* pathfinding generating new path when created new obstacle 0 Answers