How do I move simple AI in a 2D space?

I’ve been looking all over for a solution that works and maybe it’s so simple that thats why I can’t find anyone talking about it but I have my little 2D AI BP character who I would like to make move around. However the ‘simiple move to location’ function in BP is for nav meshes and using it in a 2d space doesn’t seem to work.

Whats the best/simplest way to get a 2D character to move to a new location? Specifically which function in BP.

Thanks!

There are a lot of “it depends” type answers that could be given here, depending on if your AI is like a physics driven ball, or a character-movement driven character (instead of a pawn) or maybe even a paper 2D sprite in screen space. But the overall techniques come down to two main choices:

  • You can use a nav mesh and “simply” tell the AI to go to a location.
  • You can teach your AI (or it’s controller) how to drive towards a destination.

So given you said you’re having a problem implementing a nav meshed solution, here’s how to do the 2nd way.

  1. Moving somewhere happens over time, so this is a clue to implement the following in your Event Tick function.
  2. First step is to figure out which direction you want to go. Directions are expressed as vectors. The best way to get an arrow pointing in the direction towards where you want to go is to subtract the destination position from your AI’s current position.
  3. The length of the arrow formed by that subtraction will depend on how far away the target is. This isn’t necessarily a good thing for movement purposes. So to shorten it up to be 1 unit long no matter how far away the target is, you’ll want to “Normalize” that vector’s output. Think of it like a compass needle - its always the same length pointing north no matter how close to the north pole you are.

So that’s how you steer. To hit the gas, you’ll want to have a variable you can play with to adjust your Speed. This should probably be multiplied by the Delta Seconds coming from the Tick event so that games played on computers with higher frame rates don’t see the AI going faster.

Depending on what type of movement input you want to use, you’ll now have all the pieces to get your AI to move in the correct direction towards a goal. The “Add Movement Input” node takes a Scale Value (your gas pedal) separate from your Direction (your steering). The “Add Input Vector” combines them both (by multiplying them).

In this example, the goal is a target actor stored in a variable I made called “My Target”. So this would work if you wanted the AI to move towards the player for example. But you could also do this so it goes towards a specific location or in a specific direction by feeding it a vector instead of using My Target’s actor location.

Not too hard once you’ve done it once or twice - hope this helps.

1 Like

Hey thank you so much for the reply, that was a lot more detail than I could’ve asked for. I tried the first method and it worked perfectly on the first try!

I should be able to get the rest of the AI stuff working as it’s already working for the players character, I just couldn’t find any example of what you have shown me. Thank you again for the help, my little town has functioning citizens now :slight_smile:

I have the same problem and after read this post, I’m still confusing about the “My target” node. How can i store the value of player in another bp to this variable?

Thanks alot!

So i did try your system 1l2Hawk but for an unknown reason My Ai still doesn’t move. I have a nave mesh placed and functioning with my pawn character, but not my Ai… Do you have any clue of what can prevent him to move?

I know its been YEARS, but I had the same problem you were looking to solve, here’s the solution video by someone else! :slight_smile: works brilliantly. Paper 2D #9 - Simple Enemy Movement - Unreal Engine 4 - YouTube

ive followed along and it worked with going left and right but the enemy wont go down?
do you have any ideas why?

Thank You so much it worked perfectly