Random movement in AI

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By belzierefactory

So, i’ve been working on a game for my final exam (in june), and everything works just fine godot is great and many questions I had had already been answered by others. But now I’m stuck on random movement and every single answer that was given to others does not apply to me. Lemme explain.

Right now I have a mob who is on my map, when he sees player (using raycasting) he dashes quickly towards him, but when he doesn’t see the player, he isn’t much of a threat, what I want to do is have him move randomly on the map, while making sure it doesn’t get stuck / goes in circle and also that he is aware of the “walls”. I had some ideas about it but every answer is use randi() and I can’t figure out how to do it, it goes in one direction (down left) everytime and gets stuck, any idea ?
Thx in advance

:bust_in_silhouette: Reply From: humble-coder

This can be done in a several ways, here is few examples:

  • One of the approaches is to use wander steering behavior as show in this video: Gamedev In-depth: Steering Behaviors (Wander) by KidsCanCode

  • Another one is:

  • Break level in a set of a connected convex polygons or rectangles

  • Make sure all polygons are passable by mob

  • Add a navigation graph between them

  • Randomly select an index of one of polygons

  • Save an index to a set of selected polygons

  • Randomly select a point in a previously selected polygon

  • Calculate path between current mob position and a selected point by using for example A* algorithm

  • Traverse a path

  • When mob arrives at a destination we could select another polygon and make sure it’s index not in a set of selected polygons