Simple AI detecting distance to player. 2D

How can I create a really simple AI that will chase the player if they are within a certain distance from them? Im using Paper2D for everything.

Hey Shirk, presumably all these are exposed to blueprints and have bp nodes;

You can just get the position of the player sprite, get the position of the enemy, use vector 2D functions to get the distance, direction etc, and move the enemy in the direction of the player sprite, at the speed you want them to go at. Do it per tick.

Thanks for the reply, but how could I actually go about creating the AI with paper2D? As far as Im aware I can’t create a nav mesh on sprites.

I’m not sure why you’d need a navmesh- thats for navigating across 3D. In two dimensions the route would be quite simple. And if you are updating per frame then you don’t really need a behavior tree either- you just need to detect when a collision occurs. Unless you want more complicated behavior- such as the enemy switching out of that mode when the player is no longer in view, or something like that…

But if you want to find out how a behavior tree works, check out this or one of the other tutorials;

I tried using horizontal distance, but Its not working at all. No matter what I do, its never true. If the player is within a certain distance, I want the enemy to make a sound.

http://i.imgur.com/BD61XQE.png

Your event tick is not connected to cast to player controller, which is not connected to get horizontal distance, which is not connected to branch. But, you want the distance between the player pawn and the enemy, so if you are casting to the controller in order to get the pawn, you need at least one other node to get the pawn. You probably need two- one to get the pawn and one to get its location. Your logic is really bad mate. Are there any tutorials you can do in Paper2D?

This is my first time using blueprints, I dont really know what anything does. I cant find any tutorials on the stuff Im trying to do.

Ok I’ll have a look this eve, I’m on my phone at the moment

Hey Shirk, here’s a list of tutorial videos. I think just work your way through the intro and essentials videos to start off with. There’s 9 introduction and 14 essentials videos

Once you’ve done that, there’s a series on Paper 2D

Your issue here is that there’s “two kinds” of blueprint modes “do-ers” and “getters”. I’m just making this terminology up, but the ones with the arrows under their names (cast to player, get horizontal) are required to be connected, the ones without arrows just get information.

I can see where you went wrong, you’re “getting information” to check the distance so you worked backwards to get it, but a few of the nodes you put to get the distance are “doers”. They need to get and check information to send to compare.

Simple solution. Just connect the arrows.

Although I’m not sure in this instance if you need to cast to player. I think you can just “get actor location” self & player pawn, then compare those two vectors

Also Horizontal distance, I never used that myself, not sure if that might hit some issues depending on your game. If it’s 2d sidescroller probably not. But I still prefer using “vector length” which makes a line between two vector positions you give and then calculates it.

Hope this helps

First create a “Function” name it “Player Distance” or whatever

Then create a new Event Name it “Distance Player” the drag pin set timer by function name. in Function name put “Player Distance” or whatever you named the Function.
Inside the Function “Player Distance”

Create a new Float Variable name it “Distance”
Use the branch node to create Distance Conditions like If the Player is 600 units away from the Enemy you Seek Player

Check out our Game @
The Crop Circle Warriors Everything is done in blueprints and paper 2d

1 Like