In one of our previous post, we went through the procedure where we made our player character to move over a defined path created using Spline. Now some of you must be wondering about to make an AI Bot character and move AI Character on a defined path. So in this post we will be going through the steps to achieve this.
We will be following a slightly different approach here to make our AI Bot character to move over a defined path. Unlike moving character on Spline path , here we will be setting up a path by using nodes which will act as our Bots’ targets to move to. So lets start to make our Bot character to do a simple Patrol functionality over a specified path.
From this post onward, before starting with the tutorial, I will introduce you with some important functions and terms of Unreal Engine 4 that we will be using in the tutorials. They will be very helpful to you in making future projects in Unreal Engine 4. So try to remember their functionalities so that you can use them appropriately whenever required. You can also make notes for future references.
New things to learn in this post
- Nav Mesh Bounds Volume : Whenever we use AI characters in our game, we need to define a volume within which our AI can move. This volume is defined by using Nav Mesh Bounds Volume in our level. It is shown as green surface where our AI can move. You can press “P” on your keyboard to toggle its view if you can not see its green surface currently. Check documentation for detailed use of Nav Mesh .
- AI Move To function : This functions is used to move an AI character inside a Nav Mesh Volume. It takes a pawn as input, which is our bot character that we need to move. And second input we need to provide is a Location or an Actor. Our bot will move to that location or Actor.
Steps to Move AI character on a defined path
- Very first thing we will do is create a Bot Character. Make a Blueprint class of type “Character” and name it anything you like. Here I will be using name “AIBot” for our AI Character. Now what will differentiate it from the normal player character is its controller. We will be using “AI Controller” for this character. Go to the details panel of character and search for “Controller”. You will find an option for setting AI Controller Class. Select “AI Controller” inside it. Now your character is ready to use functionalities of AI.
- After we have our Bot Character ready, we have to define a path for its movement. We will make path nodes which our character will follow. So create a Blueprint of type Actor and name it as you wish. I will be naming it as “AIStops” for this example.
Open this Blueprint and go to add component. Search for “Billboard” and add it.
- After we have this node, we need to tell this node about its next destination. So we will create a public variable of type of AIStops which will hold value for our next node. Lets name this variable as “Next Stop”.
- Now we are ready to set up our path. Drag the AIStop blueprint into your level. Place them as a path on which you want your character to move. Select first AIStop blueprint and go to its details panel. Set the value of variable that we created in our AIStops blueprint i.e “Next Stop”. You will find a dropper icon next to this variable, select this icon and then click actor in level which you want to be next stop. It is easy way of referencing to actors in level.
- Now you have created path for your bot character. Drag you AIBot blueprint into the level. Now open the AIBot Blueprint. When the level will start, we need to specify our bot that it should move to a specific actor in our level. So we will create a public variable of type AIStops, so that we can edit it in our level. Lets name it “Starting Node”.
- Now select Bot Character in your level and go its details section. You will find variable “Starting Node” here. Set its value to one of the AIStops present in our level.
- You have specified a starting node to actor. We need to move our bot to this actor when game will start. So go to Event Beginplay of your bot character. Now right click and search for “AI Move To” function. You will have to provide input for pawn and target in this function. Pawn here is our Bot character, so we will take reference to self by right clicking and searching for self. Connect this reference to Pawn input in function. . Now get reference to “Starting Node” variable and supply it as input to Target Actor inside function. It will be clear from following snapshot.
- Now try playing your level. You will find that nothing happens. This is an important step that most of the time people forget to add.
An AI character does not move until we specify an area for its movements. This area is added by using “Nav Mesh Bounds Volume” which I have explained in starting of this post. So in your map tab, look at left panel where you will find modes section. Search for nav there and drag “Nav Mesh Bounds Volume” to your map. Now specify its length, breadth , height as needed.
The green area is where our Bot character can move. It can not go outside its boundaries. If you are not seeing green area view in your Nav Mesh Volume, just press “P” on your keyboard to see it.
Now play and you will find that your bot will move to starting node that you specified in your bot character.
- Now you’ll see that Bot will stop at the node and do not move to next node. Now we will set the movements in Bot Character blueprints’ Tick event.
Get reference to “Starting node” variable again. Drag from it and search for “Next stop” variable that we create for this AIStops actor. Now right click and look for AIMoveTo function. Pawn input remain same as step 7. And target actor input is set by using this NextStop variable that we just find out in this step. When bot character will reach the Next stop, then we need to update our starting node variable. The “Next stop” will become our “Starting node” variable now. In this way loop will follow and our bot will move from one node to other. If we specify “Next node” for our last node as our First node , then bot will keep moving in the path without stopping. Check following screenshot to clear your doubts.
I have added a delay of 4 seconds which will make my bot character to wait for 4 seconds before going to next node which will look like a simple Patrol by an AI bot.
This way we can move AI character on a defined path . Let me know in comments if you are having any problems implementing this. I will try my best to solve your problems. Sorry for any mistakes you find in the post. Help me by correcting me.
Keep Learning !! 🙂