How do I check if an object is within a radius of another?

0 favourites
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • So I have this weapon in my game that shoots these lightning bolts, and I want it to bounce from one enemy straight to another if they are close. Problem is that I don't know how to this.

    I've tried using invisible big circles and pinned/moved/containered them to the enemies, problem is that they stack on top of each other.

    I've tried Clamp, but I don't quite understand how that works.

    It would be nice to do it without using the sprites, but any help at all is welcome and appreciated!

  • Use the distance() expression.

    https://www.construct.net/en/make-games/manuals/construct-3/system-reference/system-expressions

    I'm not the brightest so I just need to ask: Should I for example just do distance(-200, -200, 200, 200) ?

  • You provide the points of the two objects you are checking.

    if( distance(Enemy1.X, Enemy1.Y, Enemy2.X, Enemy2.Y) < 50) do something.

  • You provide the points of the two objects you are checking.

    if( distance(Enemy1.X, Enemy1.Y, Enemy2.X, Enemy2.Y) < 50) do something.

    I see, but I want it to be in the Enemies family, which means that I want any enemy to be checked, this includes the same type of enemy. Should I then just do distance(EnemyFamily.X, EnemyFamily.Y, EnemyFamily.X, EnemyFamily.Y) ? This seems not right due to the fact that they just repeat the same thing. Am I misunderstanding?

  • There's various ways of doing this. Cache the first coordinates, and compare those to the other enemies, or have a second enemy family, so you have two unique objects to check.

  • Maybe this can help you. this example from dop2000 share:

    howtoconstructdemos.com/simple-ai-with-pathfinding-wolves-chasing-prey-capx

  • There's various ways of doing this. Cache the first coordinates, and compare those to the other enemies, or have a second enemy family, so you have two unique objects to check.

    That first option sounds like the better one. How do I do that?

  • Drag the enemies around to see them check if distance is within 200 pixels. The example will just draw lines both ways as there is no optimization to check if a line exists already, but you get the idea.

    http://www.blackhornettechnologies.com/Construct3Stuff/Samples/DistanceWithinFamily.zip

  • Drag the enemies around to see them check if distance is within 200 pixels. The example will just draw lines both ways as there is no optimization to check if a line exists already, but you get the idea.

    http://www.blackhornettechnologies.com/Construct3Stuff/Samples/DistanceWithinFamily.zip

    I think I might understand this concept. I will try it in my own game and then report back to you, thank you very much for your help

  • Drag the enemies around to see them check if distance is within 200 pixels. The example will just draw lines both ways as there is no optimization to check if a line exists already, but you get the idea.

    http://www.blackhornettechnologies.com/Construct3Stuff/Samples/DistanceWithinFamily.zip

    Alright, so I used your mechanism and it seemed to work quite well, however I have to problems, and they're probably due to me and not you.

    1: Sometimes the projectiles of the weapon seem to target an enemy really far away instead meanwhile some enemies could be a lot closer (not close enough) and not get targeted. How come?

    2: Sometimes the projectiles target the enemy it first hits.

    I use the MoveTo behavior to move the projectiles to the other enemy that is being scanned for (if that makes sense) otherwise the bullet behavior is being used at the start. The problems probably have something to do with the MoveTo mechanic, however I do not know why

  • You'd have to post your code at this point.

  • You'd have to post your code at this point.

    prnt.sc/uov9j1

    Here it is!

    The code for the projectile is mostly your code and destroying the projectile

  • You just need to find the closest non-hit enemy then.

    http://www.blackhornettechnologies.com/Construct3Stuff/Samples/DistanceBounce.zip

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You just need to find the closest non-hit enemy then.

    http://www.blackhornettechnologies.com/Construct3Stuff/Samples/DistanceBounce.zip

    I've tried around a bit now and it still somehow does not work.

    I've copyed your code, the only things that have been changed have been the instance variables to fit mine. Yet somehow something is wrong!

    I've tried deactivating the first code you sent me (like in your most recent code) I've tried putting some code over to the other, I've tried having both of them on, and I've tried everything in between, yet still it sometimes keeps targeting an enemy far away and other wonky stuff.

    It probably has something to do with the functions. I cannot add the function on shooting the weapon because of how I want the weapon to act. I may describe how I want the weapon to act very simply right now:

    I want it to shoot out fast projectiles quickly that go straight ahead (with the Bullet behaviour) and when they hit any enemy while any enemy is nearby it should then "bounce" or rather fly straight to said enemy. They will bounce a few times before destroyed.

    So by this description your code works quite well, and you've done a good job, but I do not see why it does not work for me.

    I'm so very sorry for bothering you so much, and I am extremely grateful for the amount of help you're putting into this, thank you very much.

    Do you have any other ideas to how this could work?

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