0 Favourites

# AI Enemy Distance

• 12 posts
• Hello,

I feel like I should be able to figure this out, so sorry, but my brain is just not figuring it out at the moment.

Simply put: enemies are moving towards your character and when get close do a different action (jump at them), so am trying to compare X but am just not getting it:

So would think could use Enemy compare X so it would only select the instances of the enemy that are close, lets say withing 100 pixels so it would be if abs(Enemy-Character) < 100.

But I have to use the comparison Enemy <,>, etc. and it is not hitting me what to do as Enemy.X < Character.x +100 does not work as enemies are coming from both sides.

I am about to just add LOS to enemy out of frustration, could use an instance variable also, but thought there would be a simpler equation as a solution.

Thanks,

Chris

• Have you tried dist(x1,y1,x2,y2)?

• That makes perfect sense for measuring the distance between the two but what would the event be?

If (dist(x1,y1,x2,y2) < 100)

I have to use an event through the enemies selection so it will only select the applicable instances - I don't see how I can use that.

I guess I could create an instance variable, set it to 100, and then compare instance variable to dist(x1,y1,x2,y2).

Thank you, I wasn't aware of that. But I still feel like there should be a way without an instance variable. I like Construct but have had much frustration on the simplest things like this.

• Are you using families? If you are, you might want to make sure your events are nested under a 'for each' system event. And then you could just use the system condition 'Compare two values'

first value: "distance(enemy.X,enemy.Y,player.x,player.y)"

comparison: "< Less Than"

second value: "100"

That should check for every instance you have in the enemies family, and their proximity to your player.

• I am not using families in this case but I only have one type of enemy (so unnecessary, correct?)

I thought if I use the system condition compare two values that it would not select any instances for the action? Or will the instance from the for each carry through?

Sorry for the nooB questions, but understanding these basics will help me greatly moving forward.

Also, does for each run every tick?

Thanks,

Chris

• no worries man. I know you might not have families available to you at this point.

you're right, in this case using the system condition to compare distance will not pick a specific instance of an object. However, if you nest it as a sub-event under "For Each (object)" you should get your desired result. You can check out 'For Each" here:

https://www.scirra.com/manual/124/system-conditions - but more or less what this does is force it to apply this to every instance of the ojbect.

You also don't need to run every tick. Here's some info on that:

is-there-a-list-of-conditions-that-work-as-quot-every-tick-quot_t104486

• For each Enemy |

If dist(Enemy.X,Enemy.Y,Player.X,Player.Y)<100|Jump

• no need for foreach. if you put if dist (x1,y1,x2,y2) it will calculate towards each instance of enemy. and jump will trigger when <100 dist. if you want it to trigger once - add trigger once.

• Thank you mudmask and StormHo

no need for foreach. if you put if dist (x1,y1,x2,y2) it will calculate towards each instance of enemy. and jump will trigger when <100 dist. if you want it to trigger once - add trigger once.

From my understanding, and the posts above, this would cause the action to apply to all instances, therefore the ones not within the distance would all jump when one gets close.

• ## Buy Construct 3

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

Construct 3 users don't see these ads
• you could simply have a sprite on the main character like a zone of influence and when the enemy overlaps with it, it triggers the action

• you could simply have a sprite on the main character like a zone of influence and when the enemy overlaps with it, it triggers the action

That sounds easy to setup and would probably work out well. Although I will probably not go that route as I am trying to optimize by reducing unnecessary objects/sprites. I know this is only one more sprite but from what I've read I think the test for overlapping is more to process than test of distance previously mentioned.

I always appreciate different ways to solve problems though so thanks for the comment.

• if there are more than one player character, use for each for player character too

• 12 posts