Question by
MarkusA380 · Mar 21, 2016 at 05:38 PM ·
c#navmeshagentnavigation
NavMesh Agent won't move
Hello there!
I know, there is lots of these question out there, but I just don't find any cause for this problem:
The NavMesh agent won't move at all! As you see in the image, everything seems to be calculated correctly, but nothing happens. Here is the GameObject of the Agent:
Also, this is the attached script:
using UnityEngine;
using System.Collections;
public class TestNav : MonoBehaviour {
// Use this for initialization
void Start () {
GetComponent<NavMeshAgent>().destination = GenerateRandomPosition();
}
// Update is called once per frame
void Update () {
}
Vector3 GenerateRandomPosition()
{
Vector3 ret = new Vector3();
ret.x = Random.Range(-15f, 15f);
ret.z = Random.Range(-300f, 300f);
ret.y = 0.5f;
return ret;
}
}
Does anybody know what the problem is?
problem.png
(434.3 kB)
problem2.png
(67.6 kB)
Comment
Answer by MagnusBoll · May 11, 2017 at 10:46 PM
@MarkusA380 You sets the destination before you have a destination. The start method is the first your script is going through. So first you sets the destination to nothing and then you sets a destination, but you don't set the NavMeshAgent to that destination.
Try to set the NavMeshAgent destination in the Update method instead.