- Home /
How to create AI movement for avoiding procedurally generated objects?
I've been struggling with this for a few days trying different approaches but I end up stumbling over myself and losing motivation because I don't know where to start really. I'm trying to create a simple 2D top-down shooter type game where the player has to avoid obstacles and shoot down enemy ships.
I would like the enemy movement to behave similarly to the player, but I'm having trouble getting them to avoid obstacles consistently. Currently, they only move horizontally (scrolling backgrounds give the illusion of vertical movement) and movement is based on a single acceleration variable where 0 is stationary and 1 is their maximum speed. Each enemy casts a boxcast forward, and if that boxcast detects an object then their X acceleration will change dependent on the object's distance. So acceleration to the left or right increases the closer an oncoming object is. This works quite well, but does not account for objects to the left or right of the enemy, so enemies will smash into adjacent objects in order to avoid oncoming ones. Here is a terrible diagram to help visualise:
I should point out that the ship will only collide with the largest, brightest asteroids; the smaller, darker ones are just background objects.
It's probably worth stating that these asteroid objects spawn one at a time at a random X position above screen, and have a slight variance in their spawn intervals; meaning gaps between each asteroid can be slightly wider/narrower than others. There should always be enough of a gap for a ship to fit through between each asteroid however.
I currently don't think I'm approaching this the right way and there is a better approach I've not found. I have an idea (which I'm not sure how to implement) that I could create a dynamic path that avoids the asteroids. Maybe I could place a point to the left or right of an asteroid (whichever side has a larger gap) and the ship could accelerate towards that point along it's X axis, once the point is reached a new point in the path would be placed alongside the next asteroid and so on. I have no idea where to begin with trying something like this though.
If anyone can offer advice or point me in the right direction to read up on useful practices for AI movement, I'd be very appreciative as it's entirely new to me :)
Answer by RLin · Apr 08, 2018 at 07:20 PM
I think the best way to do this would be to use horizontal raycasts/boxcasts to the left and the right in addition to the boxcasts you already have. If either of the raycasts to the left or right hit, then the ai knows not to go in that direction. If they both hit, have the ai accelerate to the side where the hit distance is greater. You could also create a separate collider for the asteroids that does not affect physics but will affect raycasts. This collider could be larger than the actual asteroid, ensuring that the ai always leaves a reasonable margin of space when dodging an asteroid.
This was my first thought, though I did wonder if it was over-complicating things. I think I'll make another attempt with more adjacent boxcasts, but there was still some issues.
For example, in the image above, the adjacent boxcast would detect the asteroid to the right and so attempt to accelerate left. The ship is clamped to the screen boundaries and so would collide with the left side of the screen and eventually collide with the onco$$anonymous$$g asteroid above it. In the image above, the ship would ideally accelerate to the right but at a slow enough speed that the asteroid to the right would safely pass by the ship first.
Thanks for taking the time to reply, I'll continue trying out adjacent boxcasts and see what I can come up with for now :)
Your answer
Follow this Question
Related Questions
Help with 2D AI scripting 2 Answers
Simple AI In 2D - C# 1 Answer
2D Platformer Ai Movement Decision Making Problems 0 Answers
enemy AI patrol not working 1 Answer
moving AI up/down if lower/higher in Sector as player 0 Answers