Hello! I am making a strategy game where a character moves wherever the player clicks. But I want to preview the path to the currently hovered point and display it with a line renderer. I tried calling SearchPath
every frame or with a given frequency but I feel like this is pretty inefficient. Is there a better way to rapidly calculate the path with a given point? I guess you can think of it as fundamentally the same as a follow script.
Currently looks like this but it is pretty slow: 21.05.2023_11.29.13_REC
1 Like
Hi
I would recommend calculating a path once every few frames. There’s nothing inherently more performant you can do other than recalculating the path often. Perhaps if your game is tile-based you can avoid recalculating the path while the mouse still hovers over the same tile as the last time you recalculated the path.
I’d recommend doing something like:
void Update () {
if (Time.time - lastPathRecalculation > 0.05f && path.IsDone()) {
lastPathRecalculation = Time.time;
var mousePosition = ...;
AstarPath.StartPath(ABPath.Construct(ai.position, mousePosition, OnPathComplete));
}
}
void OnPathComplete(Path p) {
seeker.PostProcess(p); // Use same post-processing modifiers as the agent
if (!p.error) {
var abPath = p as ABPath;
// Update line-renderer
}
}
For more info, see Searching for paths - A* Pathfinding Project
Hey! Thanks a lot. I restricted the amount of calculations by time. But I used SearchPath() and waited for that to result in my own code instead of having a delegate. Is that okay? It seems to have the same effect.
That works, but be aware that if a movement script is using the same Seeker, then that movement script will start to follow the calculated path, which may not be what you want.