# Enemy Distance

Get help using Construct 2

### » Thu May 31, 2012 5:12 am

Hey there. I haven't posted for a loooong time. BUT, I'm making something (again) and I really need a hand. I went through about 70 of the forum pages looking for the answer I need, but I can't quite seem to find anything I'm looking for, so my question is this.

How can I make it so that if the player is WITHIN a certain distance of the enemy, the enemy say... For simplicity rotates. Like, WITHIN -10 of it's x and +10 of it's x. The WITHIN part is very important, hence the capitalization. If someone could help me out here, it would be very much appreciated. (I've been stuck on this for about 5 hours now. So it's probably really easy, but I can't get it at all.)

B
16
S
3
G
2
Posts: 57
Reputation: 2,078

### » Thu May 31, 2012 5:26 am

Create a System->Compare Two Values event. Then go to System and find the distance function. (It's under Math.) Plugin the player and enemy's X and Y properties. Example: distance(Player.x, Player.Y, Enemy.X, Enemy.Y). Then change equals to less than, and type a number in the second box for the distance (in pixels).
B
25
S
8
G
8
Posts: 71
Reputation: 5,307

### » Thu May 31, 2012 5:40 am

Oh maaaaan, Thanks a bunch I didn't realize that function existed. Sheeeesh. I appreciate you. :B
B
16
S
3
G
2
Posts: 57
Reputation: 2,078

### » Thu May 31, 2012 5:59 am

Okay so, I have it. But it only works once, I can't get it to work for multiple instances within the MONSTERS family. Any ideas?

Also, it's a platformer. So basically I want it so that once you're out of that say, 15 pixel range, it goes back to not rotating.Crayron2012-05-31 06:01:58
B
16
S
3
G
2
Posts: 57
Reputation: 2,078

### » Thu May 31, 2012 9:13 am

Here's an example I put together which I think does what you want:

http://dl.dropbox.com/u/47015043/construct2/sentry-example.capx

It's for a top-down configuration rather than a platformer, but the principles should be the same.

It uses invisible sprites pinned to the sentries which act as radar/sight. I've made the sprites visible in the example so it's easier to see the effect. Also, I've only used 8 collision points in the radar, but you could use more to make it more like a circle.
B
12
S
5
G
5
Posts: 26
Reputation: 4,001

### » Thu May 31, 2012 1:24 pm

Another approach uses an instance variable (distance) in each enemy instance, which is set every tick using the distance function.

jimand2012-05-31 13:27:26
B
16
S
4
G
1
Posts: 40
Reputation: 2,705

### » Thu May 31, 2012 3:10 pm

Great thanks a lot guys. I've got it all figured out now. Got it set up so that if you're within the range it starts, and if you're out of the range it stops. I just hope that it doesn't get too laggy or anything, considering I'll have many objects eventually.
B
16
S
3
G
2
Posts: 57
Reputation: 2,078