# How can I make AI maintain distance to a moving object?

0 favourites
• 7 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
• Hi,

I'm currently creating a top-down space exploration game, and am working on the AI side of things today. I've got AI enemies spawning from System -> Create Object and then moving to the player through fam_enemies_sml -> Set angle toward (player.X, player.Y), and moving via Simulate Car pressing Accelerate but this naturally moves the enemy to sit on top of the player object. How could I implement AI movement towards the player to within a certain distance at any angle?

Furthermore, how could I make the AI circle around the player? The only thing I can think of for the latter thus far is making four variables, being top, bottom, left and right around the player, then setting them to player.X or .Y minus distance variable, updating each tick and moving the AI to each in sequence? No idea how I'd pull that off though as of this moment.

• This sounds like a job for Rexraibow's LJpotential plugin

• Hmm, I'll have a look into it, thanks, . Might be back here with more questions, ha ha.

• I would love to give you an answer, but sadly I have been looking for the same thing. any luck with the plugin?

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• martinDrew Haven't tried it yet, ha ha, lots of other areas to work on and the enemy AI functions decently enough for the time being, as you've seen, .

• You could do it by just comparing the distance from the enemy to the player. Accelerate if the distance is greater than a radius and brake if less.

If you'd be happy with orbiting in a circle you could use the "move at angle" action on the enemy when they're closer than the radius and use enemy.angle-90 for the angle and say 100*dt for the distance.

• R0J0hound Cheers for that idea. I've done something similar using LOS to check if the enemy is in range and if so, to pull back a little so they're hit less, but wasn't sure how to check anywhere between the ships. I'll experiment with orbiting enemies as well, because I was hoping to introduce that a little later in AI development with a set of waypoints spawned around the player, which wasn't a very graceful idea.

Thanks!

• 7 posts