# Homing projectile vs multiple targets

• @SplitMindGaming If you put 1 in the If statement that is looking at the loop index, then it will apply the logic to the second object it looks at. I think the order of the loop goes from oldest spawned object up to newest. If I'm right about this, then setting it to 1 will mean the oldest spawned target will always be completely ignored by the homing missiles.

• @Aidan-Oxley lol you don’t need to answer my last question. Honestly I still won’t probably get it. You would have to really dumb it down for me by using apples and bananas as a metaphors at this point because that was all too confusing for me.

• Let me try to give you an example, for example there are 3 targets in the world. Let's call them object a b and c, where a is the oldest one and c is the youngest one (the last one spawned). Object a is a medium distance away, it's sitting 5m away from the missile. Object c is 7m away from the missile, and object b is right next to the missile being 2m away. Now the objects are in these places, let's say the homing logic is triggered right now, with the objects in these places:

The loop has target as its tag, and it sees 3 targets (hyperPad does this automatically), so the loop is going to trigger all the behaviours underneath it 3 times. The first time is run, and the loop index is now 0. It calculates the distance to object a (the oldest) first, and gets 5. Then it runs the If statement, is index equal to 0? Yes. So now set the Minimum Distance box container to 5. After this is done, it runs the 2nd If statement. Is the calculate distance LESS THAN or EQUAL TO the minimum distance? 5 = 5, so yes. Get the ID of the object we just calculated distance to (which is object a). First loop done.

Second loop, now the index is 1, and it is now looking at object b. Calculate distance, and it gets a value of 2. Does index = 0? No, so the first If statement does nothing. Second If statement, is the calculate distance less than or equal to the minimum distance? The minimum distance is currently still 5 at this point, and 2 is less than 5. So now we set the minimum distance to the calculated distance (which is 2 right now), and get the ID of this object (which is the ID of object b).

3rd and last loop, the index is 2 and looking at object c. Calculate distance, gets 7. Index = 0? Nope, it's 2, do nothing under this If statement. Is calculate distance ≤ minimum distance? 7 is not less than or equal to 2, so do nothing.

The loop is done, now continue on. The homing timer is set to home in on the last object that had it's ID taken, and this is object B, so the missile will now home in on object B, which was the closest one being 2m away.

• @Aidan-Oxley that was perfect actually. Everything makes a lot more sense now. Thanks for that.

• @Aidan-Oxley you also mentioned that loops work really fast and lag the game. Does this mean that we should try to avoid loops as much as possible?

• @SplitMindGaming well a loop basically runs the behaviours under it multiple times at once. Imagine you have a loop with 5 behaviours under it, and that loop runs 5 times, it actually runs 5x5=25 behaviours.

It'd be a lot more complicated (and not perfect), but if you have loads of potential targets to the extent that the targeting causes the game to lag, you could try splitting the search for nearest over multiple frames by creating an array of objects to search through and only searching through (e.g.) 50 each frame.

Depending on the type of game, you could also just make it so it doesn't switch targets after selecting one (only running the search once).

• @Jack8680 ahh i see. Thanks guys I learned alot today.

• You guys are literally teaching / learning university level computer science topics. So exciting to see 😭

• @Aidan-Oxley said in Homing projectile vs multiple targets:

@bosswave I made an example here:

Press the red octagon to spawn missiles, press the blue one to spawn targets. Targets can be dragged around to test missiles. These homing missiles will calculate distances once and continue to home in on the same target (rather than constantly switching to whatever is closest). The homing trigger behaviour bundle is made in a way that it can be triggered at any time to cause the missile to choose a new closest target (or the same one as before if it's still close) but I would not recommend triggering this on a fast timer if you have lots of targets.

It took me a while to get it right because there were a few minor bugs I had to work around that I might try to figure out more details for to submit bug reports soon. (Just saying this in case you see the logic and see a more efficient way to do it, you might run into the exact same bugs I did)

Also they use very simple homing mechanics obviously you can easily change them if you have your own fancy cool looking homing missile physics, just have to use the same referenced object.

EDIT: this example sucks. This one is way better: https://go.aws/2zEM8XH

@Aidan-Oxley what were the bugs?

• @Hamed Oh right I forgot about them. Uh, I'll go try remember and make proper bug reports. I think there was actually only 1 bug.