Enemy AI, turn oposite direction at the end of the platform, instead of falling

Hello, I’m working on a 2d platformer game and there’s a level where there’s alot of small/medium platforms, I would like to put enemies on the platform that move left to right, although when they are at the edge of the platform, instead of falling, they flip and walk in the opposite direction, any help / tips will be greatly appreciated, thank you for taking your time to read this post!

you could put a small collider at each end of the platform & use that to trigger a script to turn the enemy & multiply the movement speed by -1.

1 Like

I accomplished this and a bit more by creating a spawning object that spawns my mobs. The spawning object contains information that sets the mobs data, respawn times and spawns the prefab for my mob.

When it comes to it’s range, I have 2 objects attached to the main spawn object. Each of those 2 objects contains a collider, set to trigger and they act as my boundaries.

The mob walks left->right and when it collides with the boundary, the script inside it tells the mob to change directions(a setting within the mobs script).

They will bounce back and forth and “patrol” the area between the 2 boundaries.

Things change a little when a player gets in range. The mob will stop walking and start running. I have a variable I call “chasing” on the mob, and if it is in chase mode, then instead of the boundary sending the mob back in the other direction, it sets a variable inside the mob that lets it know it’s left it’s containment area, and what direction it needs to go to get back “home”. When it gets back to the boundary it left, then that boundary will trigger that it’s back in it’s space and will go back to patrolling as normal.

I also have platforms, so additionally I add a boolean for “confined”. In the event I have the boundary marked as confined, even if the mob is in chase mode, it will turn around. This is to prevent it from falling off platforms. And it’s flexible with the other boundary, so you can let it chase past it’s patrol area on 1 side, but not another.

You can also add a 3rd boundary using the same scripts. So let’s say for example you wanted to patrol an area. On the left hand side, you just want it confined as to not fall off a platform. But on the righthand side, you want to patrol a small area, allow it to chase a bit, but eventually there is a fall. So, you put your original 2 boundaries and set the left to confine, the right one with it off to allow chase. And then you put your 3rd boundary out to the next edge, and mark confined.

That’s the how the system I use works. It’s not really complicated, just a bit spaced out between the objects etc.

1 Like

Couldn’t make that work because it will flip my player too, he automatically flips if he hits a wall, how can I do it in another way, that won’t affect the player, but only the enemies? And also, the platforms are randomly generated…

make them a trigger. The player should ignore it unless you tell it to react to it.