Strange delay on initial pathfind, all subsequent ones work fine

Hi, thanks for this amazing program. I am making an isometric “click to move” type of game, like Baldur’s Gate, Diablo, etc. When you click on the ground, I perform a raycast to the clicked point, and the player then moves directly there. I am using your pathfinding so that if the player clicks anywhere at all in the level, the player will automatically navigate corners, avoid obstacles and find a way to this point without walking into walls. This all works perfectly. Well, almost; the only downside is that sometimes when the game starts the very first click takes a long time to register, and the player just walks in place for about 5 seconds before the pathfinding “kicks in” and he navigates to it normally. Almost like he’s stuck against the ground or something and then suddenly gets “unstuck”. All subsequent clicks work absolutely fine. This problem is not 100% reproduceable which is even more annoying, it happens maybe 10% of the time completely randomly (it doesn’t even seem to matter how close or far away I click - just completely random). This is using gridgraphs btw.

I have tried everything from starting the player slightly above the ground, to tinkering with some of the settings and had zero luck so far, but I am quite new to this. I even rewrote all of my mouse click and raycast code from scratch in a different way, presuming I had put some tiny bug in there by accident, yet the exact same thing happens with the new code too.

Happy to post the game if that helps, but could really use some advice or ideas on things to try! Thanks hugely in advance for any help.

I also have very slow inital pathfinding (within the first few second of the game) on a grid graph. Takes about 5 seconds to return a path, as you say.
Strangely, on systems that are very powerful (where the game gets 300fps+) it’s not nearly so bad. I guess Unity is doing other stuff during the first few second of scene load and Astar is waiting for it?

Thanks for the message, good to see someone else has somewhat similar problems (I guess!). Does your slowness happen every single time, or is it sparodic like mine? 9/10 times it’s just fine here.

It’s strange, as when it does take a while, mine still returns a seek time of just a few milliseconds in the console, even if it took 5 odd actual seconds to perform it on screen, almost like something else is holding it up rather than the actual seeking?

I’ve since tried to re-code my mouse click routines yet again (third time), and am still getting strange, random delays on that first move.

Completely stumped, it’s holding up my whole project ;(

Whoops, didn’t mean to click “Accept Answer”. Still unanswered.

Take a look at this thread:
http://arongranberg.com/vanillaforums/discussion/342/occasional-large-delay-when-searching-path#latest

Curious if you have the same problem …

Thanks for the reply, I had seen that post but nothing mentioned seemed to help here unfortunately :frowning:

It really sound like this http://arongranberg.com/vanillaforums/discussion/342/occasional-large-delay-when-searching-path#latest
Are you sure you have tested everything said there?
Also are you using multithreading? If not, try switching to multithreading, if yes, try switching it off. Does it change anything?

Thanks for the help, you were of course completely right, and the solution to switch the code to Start rather than Awake worked perfectly!