Move AI character on a defined path (AI Character simple Patrol)

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

  1. 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.
    Move AI Character over a path
    Move AI Character over a path

     

  2. 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.Move AI Character over a path AI simple Patrol
  3. 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”.

    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol
  4. 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.
    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol

     

    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol

     

  5. 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”.
    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol

     

  6. 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.

    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol
  7. 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.
    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol

     

  8. 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.

    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol

    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.

  9. 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.

    Move AI Character over a path AI simple Patrol
    Move AI Character over a path AI simple Patrol

    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 !! 🙂

 

Social sharing
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on TumblrEmail this to someone

20 thoughts on “Move AI character on a defined path (AI Character simple Patrol)”

  1. Thanks, this method of moving my AI worked great. I wanted to use the spline method since my path breaks into a fork and then comes back together at the end and the movement from point to point would be smoother but this way works nice too. I just removed the delay so the movement from one target to the next doesn’t look as slow.

    1. I define a spline path to move my first player pawn actor (for 2.5 sidescroller style game) and i want to define some point of this spline as target point for my AI Bot to patrol, and want this AI BOT move along the same spline to reach the targets.
      Do you have any suggestion?

  2. I cannot get the AI to move to other nodes because I cannot update the ‘StartingNode’ with the next ‘NextStop’. This seems to be because when highlighting the ‘NextStop’ node in the final blueprint, it states ‘Variable is out of scope’ meaning it cannot set it. Why is this happening?

    1. I also do not understand how ‘NextStop’ is meant to be updated. The system seems to update the ‘StartingNode’ to the ‘NextStop’ node. But how is it decided what ‘NextStop’ node to choose? I don’t understand how a specific node is selected.

      1. Okay, so you have to click on each individual stop you place in the editor and set its “next stop” value to the next node you want the character to travel to. Hope this helps!

    2. I am having the same issue. I am not sure if this tutorial is broken since UE4 has been updated, or why it isn’t working.

  3. And if I want set key for moving…And when i press this key bot start moving. How to do it?
    And another question. Is there a possibility use AI Move To in my charachter? In “simple move to” have not a parametr “acceptance radius”, and i want that my character move to exactly location, what i want when press key…

  4. i had a weird problem, but i solved fairly easy, don’t know if somebody had the same, just in case i am willing to share my solution, hate to see somebody struggle.

    I am currently working with version 4.11, for some reason, when he reaches the target the delay sends ok, but shortly after, and calculated exactly the delay time, it sends a second time, not fired by the succes pin, but realy from the delay on it’s own.

    i realy did not want to dig further in why this happen and i just figured a simple solution at hand

    i added “after” the delay a flipflop, wich after succes > delay switches on from A, so when the eronous delay sends again (wich is unwanted) , the delay switches the B from the flipflop, wich in turn is not connected to anything, and the set to startnode stays clean to the next succes.

    hope sombody finds this usefull, for me it realy solved alot of things

    happy to help

    1. @UT3_BullDozer. Thanks for sharing your experience with us all. Hope it will help someone out there.

    2. @UT3_BullDozer
      Hi !
      Can you please post an image for the AIBot Blueprint with (flipflop) you had talked about earlier..!!?

      Thank you

  5. Hi!
    I have 2 problems which prevent me to implement the tutorial:

    1- In the AIBot blueprint I couldn’t connect the [Starting Node] with the Target Actor in the [AI Move To] function… when I drag to connect it said [Boolean is not compatible with Actor Reference]

    http://imgur.com/IGmHaXg
    ———
    2- My AIBot is missing dropper icon & the rest icons shown in your images…

    http://imgur.com/ygTWXY6

    Thank you

    1. Hi Saad, for the first question, you can not connect because there is a type mismatch. Because you’ve made your Starting Node as Boolean. You need to change its type to an Actor, i.e AIStops in case of this tutorial. Because our Starting Node represent AIStops type actor, and it is not a boolean value.

      Your second problem will also get solved when you fix the first one.

      Thanks

      1. Thanks for reply 🙂 it was really helpful for me as a newbie…

        The only problem I have now is how to make the AIBot move to the other AIStops in order (which was explained in Step 9),

        In the AIBot Blueprint I couldn’t get the “Next Stop” while Searching in the “Starting node” variable unless I uncheck the [Context Sensitive]…
        and this took me to the main problem, which said [Actor Reference is not compatible with AIStops Reference]

        I hardly tried to fix it, but no way!!

        Here are the screenshots showing my case… may somebody help

        AIStops Blueprint
        http://imgur.com/whGFU9c

        AIBot Bluprint
        http://imgur.com/vycCtpx
        http://imgur.com/blO1cpI
        http://imgur.com/tVGzZoe

        Thank you very much for your time

  6. Hi again !

    I just want to tell I had found the solution for my last problem.. and it is:

    Both Blueprints variables should be of type [AIStops], Not of type [Actor] as I had used before ..!!

    Thank you

  7. i cant get “Get reference to “Starting node” variable again. Drag from it and search for “Next stop” variable that we create for this AIStops actor.” to work wont allow me to connect the 2 together, comes up with actor reference is not compatible

Leave a Reply

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