AI in Godot

Making an Enemy AI in Godot Game Engine

At the start of this year I started working on getting an Ai set up that could use on various projects.

Up until this point I really had focused all my learning on character control and cameras. Plus all the basics that you need to have.

But my knowledge on AI was very lacking. I actually left out enemies in the game I made over christmas because I wasn’t confident in doing it and I thought it would take too long to implement.

I was half right. I’ve been at this for three months and she’s not looking too pretty.

With absolutely no idea, I checked online on write ups on how to control enemies or AI or anything that’s not the player.

I came across this article from kids can code on path finding in Godot. Kids can code has a lot of great articles on how to set up all sorts of different things, I’ll link it in the description.

In hind sight, this wasn’t the right “path” to go down and ended up doing something else but it was useful in learning the fundementals of AI and how to think about AI conceptually.

I had such a little idea on how things worked getting this set up really helped.

In terms of getting the enemies set up:

The First thing I did was set up a good finite state machine for these different states that the AI was going to be cycling through. GD quest has a free base structure online that you can clone from their git hub that is really good. I can’t stress this enough, if you haven’t started using state machines in your game they are such a game changer.

There was going to be and Idle, Patrol, Chase, Attack, Stagger and Die. I started with just trying to get the idle and patrol and worked up from there.

The only thing set up in idle is a timer that sends a signal on is end to proceed into the patrol state. The time between idle and patrol is randomly set between 1 and 7 seconds. Just to give it the illusion that it has a mind of it’s own.

For the patrol state. I painstaking set up the path controls. Which I mentioned before I never ended up needing but the code for getting the path was still use full.

Using a path node will get you movement along a path (I know right) that’s defined in the editor. Which is good for platforms, and objects you want to follow a specific path. What I actually wanted was for the enemy to move around a confined area to point that’s chosen randomly (or is it).

I went with using a navigation mesh instance instead. Which was ultimatly the simpliest method to get this working. Once I had the point where I wanted the enemy to move to I simply used the built in method “get simple path” which returns a pool vector array that I can use to drive my enemy to what ever location that is set. Doing this allows the nav mesh to create a path that takes into account the environment, so we avoid any obstacles and don’t walk off any edges we’re no supposed to.

Once I realised what I wanted I initially just set up the enemy to chose a random point on the path I had made. Which worked just fine. Then later on, in an effort to simply things I simple chose a random point from a vector 3, which did the exact same thing on screen but required fewer nodes.

I can still see where having a path would be helpful, like if I wanted the enemy to patrol a perimetre and I still wanted it to stop every so often to look around then it would be what I would use. So I’m still glad I learned that.

watch on youtube

Check out Kid’s can code article here

Also Check out my FPS Template


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *