Need help with 2d enemy AI

I need help setting up an enemy ai where the enemy chases the player around a two dimensional level with platforms and ladders which they can both climb. The only existing example of this that I can think of is Loderunner. I have been thinking of trying a waypoint system buy I’m not sure if it’ll work.

Enemy AI like that is a hefty undertaking.

You’ll need a pathfinding solution, unless your cool with your enemy hugging walls until he reaches you.

I’m no expert, but start doing some research into A* (“A Star”).

I’ve seen the A* pathfinding. How would I go about applying it to a 2D game?

first of all I don’t know why people always go crazy into going trying weird things like pathfinding first lol.

here are some tips in order to work with making your own AI so you know i normally use c# and i’m going to do things inPseudo so i’m probably not spelling things right!!

first start with the ai just chasing you before anything else, now you could or one use the character controller for it or rigidbodies.
to make it simple just put a rigidbody to your AI, some collider that’s not the mesh collider, so maybe a caupsule or box collider and just freeze the rotations on the rigidbody setting to start.

now to start you will need a variable of type transform to get your target for example public Transform target; (c#code)
for movement you could use the Vector3.MoveTowards function from the unity scripting reference
and for rotation to make it super easy just use transform.Look at and you need to lock probably the x,z so only rotates on Y
one example would be transform.LocalEulerAngles = new Vector3(0,transform.localeulerangles.y,0);

once you research on those you should be good on making it at least chase you, now if you want something like making it climb a ladder you could put a collider on the ladder maybe set to trigger and if the enemy touches the collider play the climb animation maybe and make it so it’s y position goes up untill it’s out of the collider and towards the target.

if you want to make it jump and objstacle you could or use a raycast or a collider whicever way is comfortable to learn so a super easy way could be parenting a empty gameobject with a collider set to trigger on the enemy and put it in front of him making shure it doesn’t touches the enemy and if that collider touches another one that’s not the player make it jump so maybe use his y position to make it go up or y and his forward so it doesn’t just jump up lol
jump forwards instead lol.

1 Like

The problem with having the enemy just climb the ladder automatically, is that now the player simply has to hid behind one and he’ll be untouchable.

Secondly does anyone have any experience using A * ? I’m trying to use the grid graph except rotated -90 degrees on the X-axis but, so far no luck

Any help would be nice?

This is how I would do it:

Make some waypoints and trace routs around the map. With those waypoints build a graph. Once your IA gets a target it should:

1-Get closest waypoint to target
2-Get closest waypoint to IA
3-Travel to closest waypoint to IA
4-Travel (using the waypoint path) to the closest waypoint to the target *
5-Chace the target until something happens
6-Repeat

I am trying to build the same thing for a game im doing but i cant find any algorithm that would go from waypoint “A” to waypoint “B” through the shortest waypoint path

I made a video to better explain my problem. Keep in mind I made the scene in the video for the purpose of test the possibility of using A* for a 2D game, so forgive me if it seems a little basic.

http://www.livestream.com/zedavis/video?clipId=flv_cfeb8751-8e6e-425e-adf7-a6e859bb75c6

I forgot to mention it in the video but the purple rectangle is a ladder.

I’m also looking in to/ playing around with waypoints, as Dydra suggested.

I don’t know if it can help you, but here is the project I’m currently working on. Nothing fancy, I just wanted to try AI a bit.

Package : http://berengermantoue.fr/unitygames/AI_BerengerMantoue.unitypackage
Test on webplayer : http://berengermantoue.fr/unitygames/ia.html

There is a lot of scripts, so if you don’t want to read everything, the path creation is in TechniquePathfinding.cs and TechniqueAron.cs.
I’m using A* from AronGranberg. Most comments in the code are in french though, sorry if you don’t understand it. If you want more info on it, just ask.