Support Forum

Can't Get AI With Colliders To Turn Corners

Hello! I’m experimenting with this, and I’m a pretty big noob with A Star, so sorry if this is an obvious one but I’m having the following issue:

https://i.imgur.com/vSjKX7H.gif

The path is being calculated fine, and the agent is moving along it, but when it reaches a collider it needs to go around, it’s only able to do it when it comes from certain angles.

On other angles (as shown in the gif) it gets stuck when both colliders meet.

I was under the impression that the sort of buffer distance between an obstacle (in this case a gameObject with a collider and an unwalkable tag) could be increased but I can’t seem to find how to do so. I tried messing with the Radius variable in the AIPath script but it doesn’t seem to change anyhting.

Any tips with this? I’m happy to post more details if necessary.

Hi

It looks like the walkable surface of your graph is way too close to the edge. Try increasing the A* Inspector -> Grid Graph -> Diameter setting. Or alternatively the erosion iterations setting, but that one is less versatile.

Hello! Thanks for the tips, that’s working well for the objects in the scene that use the layer system, but For the shown obstacle in the gif I was actually using the tag system.

That white box has a GraphUpdateScene attached with an unwalkable tag. This seems to be unaffected by the settings you suggested.

Maybe I’m going about this the wrong way though.

What I’m trying to do is have a system where I can place objects down in the scene during play, and they will become obstacles for the AI. In the past I was doing it with the layer system and then through the code I was rescanning the graph during play, this worked, but it made the game stall for a few seconds every time I had to rescan the graph.

I was under the impression that the tag system was a way of doing this without having to rescan the graph, though maybe I’m wrong.

Any tips on this? The suggestions you made are working for my non-tag objects, which makes me think I should be using that for unwalkable areas, but I need a way to rescan the graph during play that is faster performing.

If you were re-scanning the whole graph, yeah that would be slow. You can attach the DynamicGridObstacle component to anything (with a collider) what you want to move/create/delete during runtime and it will take care of only updating a small region around that object. Take a look at the example scene called Terrain (Example2).

See also https://arongranberg.com/astar/docs/graph-updates.php

1 Like

Oh the DynamicGridObstacle is really great, it’s working really well! Thanks for that.

In regards to the other issue, I removed the tag stuff and now I’m using just the obstacles and DynamicGridObstacle and it’s working better, but I still have the agents occasionally getting stuck on other colliders (like trees and rocks) on their way to the destination.

Right now, they are trying to follow the player, and once they get stuck, I can get them unstuck by moving the player to a different angle, but they can’t seem to get themselves unstuck.

Any other tips on this?

Hi

Do you think you can post a video of them getting stuck? There are several possible causes, but usually it’s easy to disambiguate using a video. (make sure the graph and the agent’s path are also visible)

Thanks for the reply! So I tried to reproduce the issue and I couldn’t but I may have found what is going on:

When I went to turn the graph visuals on I noticed it wasn’t coming, and after hitting Scan, it all came back on, and after that I no longer had the issue. So I guess the graph wasn’t scanned, which would explain why they kept getting stuck.

The thing is, I had scanned the graph before, so I’m not sure why it wouldn’t be scanned. Any idea what might have happened there? I’m gonna keep an eye out to see if it happens again.

Usually the graphs are scanned every time the game starts.

Ok so I think I figured out what’s going on!

So if I scan the graph (with the scan button), it works great, the obstacle layer is read in as obstacles and the pathfinding works really well.

But if I then go and make changes in my game code, and Unity recompiles, after recompiling, if I hit Play the graph is not scanned with the obstacle layer, and so everyone gets stuck since they don’t have an obstacle layer to avoid.

Not sure if this is intentional or what, but it seems I have to manually rescan the graph after the Unity editor compiles.

Here is a gif of my Scanning the graph, then making a code change, Unity recompiles, and then I hit play:
https://imgur.com/a/GIKRLo4

Now that I know what’s happening it’s not a big deal, but I was really confused for a while there. I’d love to know if this is intended, and I was just using it wrong, or if there might be something wrong here though.

Hmm, do you have reload scene disabled in your editor settings?
The package supports disabling ‘reload domain’ but ‘reload scene’ must unfortunately be enabled because otherwise unity breaks too many assumptions. In the beta there is a big scary warning in the inspector if you have ‘reload scene’ disabled.
image

So I did not have reload scene enabled. I now changed it to enabled, but I’m having the same behavior, where I need to hit “Scan” manually after every time the Editor compiles if I want the obstacle layer to be scanned with the graph.

EDIT: Could it have something to do with the fact that I have multiple scenes loaded? I have my game object with the Pathfinder component on my persistent scene, and the other scenes are the ones with the obstacles.

When I hit scan myself it works fine, but maybe there’s an issue when it goes to scan automatically?