Completely new with A * Pathfinding Project, I want to move my character with a simple click to move script.
With NavMeshAgent it would be something like this:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit , 100)) {
nav.SetDestination(hit.point);
}
What is the equivalent of this code using A* Pathfinding Project?
Hi
This is the equivalent:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit , 100)) {
ai.destination = hit.point;
}
Assuming the ‘ai’ variable is any included movement script (e.g AIPath/AILerp/RichAI).
For improved responsiveness you may also want to make the ai recalculate its path immediately instead of waiting until the next scheduled path recalculation. You can do this by calling
ai.SearchPath();
Mmm it works, thanks, but now if I repeat the code every frame, sometimes the path is not calculated, why happens this?
[code]void Update() {
if ((Input.GetMouseButtonDown((int)mouseButton) || Input.GetMouseButton((int)mouseButton))
&& GUIUtility.hotControl == 0) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit , 100) && lastPosition != ray.GetPoint(hit.distance)) {
ai.destination = ray.GetPoint(hit.distance);
ai.SearchPath();
lastPosition = ai.destination;
}
}
}[/code]
P.S: (Solved by read here: Tell AI to immediately find a new path and use it)
And I also take the opportunity to ask, how can I make my “units” not collide with each other?:
Hi
Path requests are asynchronous, so they may take a frame or maybe more to calculate. If you tell the agent to recalculate its path every single frame it will never be able to finish calculating a path before you tell it to throw away the current calculation and start over. You can see that it has warned about this in the log. It says “Canceled path because a new one was requested”.
I recommend that you do not call ai.SearchPath if you update the destination every frame. The AI periodically recalculates its path (controlled by the repathRate field).
Take a look at local avoidance: https://arongranberg.com/astar/documentation/dev_4_1_14_3886c2bf/localavoidance.html
A simple view is not what I’m looking for.
What I want to do is that units collide with each other dynamically without moving them when colliding. Like when avoid obstacles but in units.
Is possible to make some like this?