Distance between objects

Get help using Construct 2

Post » Sat Jan 14, 2012 8:41 pm

Hello,

I'm working on a platformer game and started working on AI for enemies. I want a certain characters only act when player gets near them. I know this has something to do with the Distance expression but I'm not really sure how to use it. What condition should I use? What kind of code does it need?
B
15
S
5
G
4
Posts: 214
Reputation: 3,124

Post » Sat Jan 14, 2012 9:14 pm

As you know there is a distance expression, its asking for 4 values: x1, y1, x2, y2.

Every sprite has a x and a y position, The distance expression helps determine the distance between two sprites.

So for example if you want to see if the player is near the wall using distance you can do something like this:

add a system.compare 2 values event.

Then on the top value do something like this but with the objects you want to compare:

distance(Player.x, Player.y, Enemy.x, Enemy.y)

then add a if less or = to 100, that will see if the player is within a distance if 100 to the player, Then add your other events/actions to do what you need to do

Let me know if you need anymore help
B
29
S
12
G
7
Posts: 740
Reputation: 7,839

Post » Sat Jan 14, 2012 9:16 pm

I made this commented capx a while ago.
I hope it can help you.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Sun Jan 15, 2012 12:21 am

Thanks, both of you! The information was really helpful. I finally can create some a bit smarter enemies with this.
B
15
S
5
G
4
Posts: 214
Reputation: 3,124

Post » Sun Jan 15, 2012 12:26 am

No prob, anything other problems let me know I'll be happy to help
B
29
S
12
G
7
Posts: 740
Reputation: 7,839

Post » Tue May 29, 2012 5:31 pm

[QUOTE=Kyatric] I made this commented capx a while ago.
I hope it can help you.[/QUOTE]

@Kyatric

I'm working on a game were using this distance detector .capx, I have multiple enemies and not all of them trigger all the time. Do I have to use UID? somehow.

Nvr mind I just set an ID variable to go to 1 on detection and used that and trigger.Erndog2k2012-05-29 17:40:06
B
27
S
11
G
1
Posts: 246
Reputation: 5,048


Return to How do I....?

Who is online

Users browsing this forum: Colorfish and 4 guests