- Home /
Can't Give Click to Move to Nav Mesh Agent ?!?
I created this Click-to-move script for my NavMeshAgent but it doesn't seem to work properly.
It will occasionally move to my click location. But, usually, my NavMeshAgent will do nothing. Sometimes I have to "click" 20 times over the course of many seconds and it just sits there. Sometimes it will move on the first click.
I've checked my mouse and it seems to be working properly. Can't figure it.
Here's the code if that helps: {
public Vector3 GoToAllyPoint;
NavMeshAgent agent;
void Start ()
{
agent = GetComponent<NavMeshAgent> ();
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
{
agent.SetDestination(hit.point);
}
}
}
}
I stripped off every component I could and I still get the same problem. Hmmm.
I was thinking it may be because I'm setting the destination inside an Update but that is the same way the example in the manual shows it...from inside an update(). Hmmmm.
Still working on it. Can't figure it out yet.
Answer by Topthink · Dec 15, 2016 at 07:54 PM
Okay, I re-baked it using different settings. Works fine now.
I have no idea why.
But it works exactly as I want it to now.
Answer by Remjie · Dec 16, 2016 at 01:45 PM
Hi @Topthink, i was trying you code and everythings is fine. Dont you forget to bake the navmesh maybe?
Try something : open new project, add a plane, bake his navmesh then create a capsule, add it a navmeshagent and your script, and so tell us if that work with this few component.
I got it to work as it was coded above but I had to re-bake it first. I'm not sure what the problem was. I baked it the first time...not sure what happened there. I actually baked it a couple more times with different settings and when I tried it again, it worked fine.
It might be something I did or didn't do. But it is fine now.
Thanks for your help.