How to check if there is a straight path to target (layer grid graph)

Hi, I was wondering if there was a way to check whether or not path from seeker to target is straight (barring traversable elevation changes). That is, ‘are there any seams in the grid graph between myself and target?’ or, ‘if I walked straight toward my target, would I reach it?’. Here is an illustration of what I’m trying to check:

I would use Physics.Linecast, but that wouldn’t account for things like pits or major elevation changes. I was hoping that GridGraph.Linecast do this, but from my tests it seems to only pick up obstacles, not seams or gaps in the grid graph.

I’m making a game in which the player moves very quickly, and so it would not be economical for each enemy to pathfind to the player’s location at all times. instead, I would like to check if there is a straight path to the player each frame. If so, simply turn toward them; no pathfinding required. If not, only then would the enemy pathfind to the player’s location.



Are you using LayerGridGraph.Linecast and not GridGraph.Linecast?
I haven’t tested that code in a while, but it should definitely do what you are asking for.

1 Like

edit: I was using LayerGridGraph.Linecast after all. I would take this reply down, but I’m leaving it so that the conversation makes sense

double checks code:confounded: :weary: :sweat_smile:
looks like that was my problem. Thanks, and sorry for wasting you time.

It may be worth giving an error when I try to call GridGraph functions on a LayerGridGraph. Currently the function runs, but just gives weird results

triple checks code…
actually nevermind; I WAS using LayerGridGraph.Linecast after all (I got confused because I named my variable of type LayerGridGraph ‘gridGraph’, which on reflection was a very dumb decision on my part). currently LayerGridGraph.Linecast returns true from across the map


Hm. I notice now that I there is a typo in the documentation. It is correct for the GridGraph implementation but not for the LayerGridGraph.

LayerGridGraph says (incorrect): Returns if \a _b is visible from \a _a on the graph.
GridGraph says (correct): Returns if there is an obstacle between \a origin and \a end on the graph.

So returning true would make sense since there is an obstacle in the way. Sorry for the confusion.

Ok. I just went through the LayerGridGraph linecast code. Wow. That was some buggy code.
I found lots of cases where it really didn’t work at all like it should.

I have rewritten the grid graph linecast and the layer grid graph linecast to use the same implementation and I have added unit tests for both of them. I think I have a fairly good solution now.

There are unfortunately changes to a lot of different scripts so I cannot just send you a single updated, but it will be included in the next update.

Just wanted to check in about how soon I can expect that update, since the LayerGridGraph Linecast bugs are keeping me from having things in a ‘play-testable’ state. Thanks!


Just uploaded 3.6.9 (which is essentially 3.7, I’m just uploading this while I prepare for the asset store release).

1 Like

It works!! thanks for your time :smile:

1 Like