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);
}
}
}
}