The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hello!

I want to make my NPCs move randomly but smartly on the map, avoiding collisions when they can.

For now, I have a TileMap for the ground tiles, and an other one for objects that can be placed on top of that (fences, rocks, ...).
I'm good for the part of setting a random destination point, I implemented a flood fill algorithm from the NPC and saved every tiles when it can move to (so I just have to pick a random one in that array).
I tried to use Navigation2D to make NPC move randomly to a position, which works great except it does not handle collisions with my object TileMap.

What would be the simplest way to make NPC "smartly" move to the destination while handling collisions? I have a lot of fences that will block NPCs (animals) from moving, so I can't just make them stupidly move and collide until they find a way.

I've seen some leads like subtracting collision shapes from navigation polygons.
Also, knowing that I already have every tiles coordinates the NPC can move to, I suppose I could build a moving algorithm from that, but I wonder if there is a simpler way of doing what I want...

Thanks!

Godot version v3.3.2.stable.official
in Engine by (21 points)

This is a really bad solution, so this isn't an answer, but I just draw manually draw the navigation polygon with a margin around collision shapes so that the NPCs will never collide with anything in the first place.

Actually never though of that, I'll give it a try, thanks!
But would like to hear about a sort of "official" solution that should be used for that, if anyone knows any.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.