[Solved] Moving onto the next object in a group with the nearest object condition


I am not sure if what I am doing is possible or if I have gone around it the wrong way, but I thought it would be best to ask here.

I have two object groups, deployable_units (player) and enemy_units (AI). I have the player units track any enemy_units that come into range and fire on them depending on what they can attack (Ground only, Air Only and Air and Ground). This does work, however it leads to all units of the same type tracking onto the first enemy_units within range.

My goal was to have all units track enemy_units, however prioritize attacking the one closest to them first, so I added simple nearest unit condition check. Originally I thought this worked, however I noticed when I have multiple different enemy_units types on screen, only a single deployable_units type would attack (eg: only the ground only deployable_units would attack until the closest enemy_units was a different type (eg: air))

Black - Large enemy_units = Flying
Red - Small enemy_units = Ground

Green - Large deployable_units = Attack Flying Only
Green - Small deployable_units = Attack Ground Only

No Nearest Unit Check

As you can see, they’re all attacking however the smaller deployable_units on the right is ignoring the closer enemy_units as the enemy_units on the left was first to be engaged (or within the object group)

With Nearest Unit Check

Now that the small enemy_units is closer to the large deployable_units, it wont attack as the large deployable_units can not attack the smaller enemy_units

Events For Targeting

What I am trying to achieve is if the closest enemy_units to the deployable_units does NOT match the variables (eg: large deployable_units trying to attack a small enemy_units) move onto the next closest enemy_units within the deployable_units range.

Any assistance/ideas would be greatly appricated.

1 Like

first of all i would like to praise you for the well detailed description! :clap: and I message every newcomer, this is how they should formulate a request for help!

but let’s get to the problem.
if I were the one doing this game, I would put the logic together a little differently.
first, I would put the different types of opponents in separate groups. for example : Air_units, Ground_units. than each player unit will pick nearest enemies from that group only, wich it can be shot. the problem with this is the units, wich can be shot enemies from more than one group. you can put the same object as many group as you want. so a mixed group(s) should also be created. so the point is: each player unit must have a group that includes only(!) the opponents it can shoot at. than pick nearest from that group, ect…

second, I cant see the object timers in your code, but I would check its state earlier in the logic. in other words: only that player unit look for target, wich can shot right now. but if the units have a high rate of fire, this is irrelevant.

so the game logic would look like this to me:

for each player unit wich object timer is greater than: object variable “fire rate”
pick nearest enmy from group: object variable “enemy group”
if the picked enemy’s distance is less than: object variable “range” :arrow_right: than rotate, shoot, reset object timer, ect…

! it’s just theory, I haven’t tried it in practice !

1 Like

Hi Gyuszko,

Thanks for the response. I had thought about separating enemy_units based on whether or not they were air or ground, but I didn’t consider that it would make tracking much simpler.

I now have 3 enemy groups enemy_units, enemy_units_flying and enemy_units_ground with each type of deployable_units only targeting the nearest valid unit which works perfectly. I deiced it would still be best to keep the enemy_units that includes all enemys as there are times I want to perform an action/check on all enemy units.

Some times its best to get some fresh eyes on what might be a simple solution, thank you Gyuszko!

1 Like

I’m glad my post helped you a bit. regardless of this, my logic is still different from yours. Of course not certain that I am right.

i don’t think it’s a good solution to start with distance measurement. I don’t know in advance how distance measurement works if you specify an entire group. either way, I’m sure that’s not a good method. I’m sure if you want an accurate measurement, you need to pinpoint the two endpoints. in this case picking the two objects: the player’s unit and its target.

maybe it seems to work well that way now, but i’m sure it won’t be good in every situation.