# AI Enemy Distance

Discussion and feedback on Construct 2

### » Sun Jan 03, 2016 1:42 am

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
B
46
S
27
G
11
Posts: 529
Reputation: 10,682

### » Sun Jan 03, 2016 2:28 am

Have you tried dist(x1,y1,x2,y2)?
B
248
S
30
G
13
Posts: 1,794
Reputation: 18,909

### » Sun Jan 03, 2016 2:44 am

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.
B
46
S
27
G
11
Posts: 529
Reputation: 10,682

### » Sun Jan 03, 2016 2:57 am

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.
B
83
S
23
G
10
Posts: 388
Reputation: 10,407

### » Sun Jan 03, 2016 3:47 am

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
B
46
S
27
G
11
Posts: 529
Reputation: 10,682

### » Mon Jan 04, 2016 6:59 am

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
B
83
S
23
G
10
Posts: 388
Reputation: 10,407

### » Mon Jan 04, 2016 3:12 pm

For each Enemy |
If dist(Enemy.X,Enemy.Y,Player.X,Player.Y)<100|Jump
B
17
S
4
G
1
Posts: 203
Reputation: 1,583

### » Mon Jan 04, 2016 8:42 pm

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.
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
43
S
14
G
12
Posts: 627
Reputation: 9,457

### » Tue Jan 05, 2016 12:30 am

saiyadjin wrote: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.
B
46
S
27
G
11
Posts: 529
Reputation: 10,682

### » Tue Jan 05, 2016 5:04 am

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
B
48
S
10
G
6
Posts: 232
Reputation: 8,106

Next