Distance between objects

  • 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?

  • 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 <img src="smileys/smiley1.gif" border="0" align="middle" />

    Let me know if you need anymore help <img src="smileys/smiley4.gif" border="0" align="middle" />

  • I made this commented capx a while ago.

    I hope it can help you.

  • Thanks, both of you! The information was really helpful. I finally can create some a bit smarter enemies with this.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • No prob, anything other problems let me know I'll be happy to help <img src="smileys/smiley1.gif" border="0" align="middle" />

  • I made this commented capx a while ago.

    I hope it can help you.

    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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)