- Home /
Click to Move With Navmesh
I am trying to use the navmesh path finding system in order to move my units in game. but i am facing a few problems.Physics.Raycast is not accurate with navmesh. the point where the raycast hits is not where i click sometimes. my terrain is a plane. i baked the navmesh on it. also another problem i am facing is that no matter how many clicks the Navmesh.Destination is locked for some reason. i call the method ClickToMove on a derived Class in the Update Method.
protected Ray ray;
protected Vector3 target;
protected RaycastHit pointHit;
protected NavMeshAgent myAgent;
private void Start() {
intializeVariables ();
}
//TODO placeholder method.
protected void ClickToMove() {
if (Input.GetButtonDown("LeftClick")&&Physics.Raycast(ray,out pointHit)) {
target = pointHit.point;
myAgent.SetDestination(target);
Debug.Log(myAgent.destination);
Debug.Log(pointHit.point);
}
}
protected void intializeVariables() {
target = transform.position;
myAgent = GetComponent<NavMeshAgent> ();
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
}
Comment