Pick Nearest Problem

I have many enemies patrolling my Scene. If my player touches their range of vision then they shoot at the player.

The problem you see, is that I don’t want all enemies shooting at the player, I only want the enemy whose range of vision is touching the player to shoot at him.

My code doesn’t seem to accomplish that:

The repeat each enemy1 limits the picked objects to 1 instance at a time. So, by default, each enemy would show as being the closest. Since you already know which vision(s) are in collision, you just need to match it to the enemies. I would change the for each enemy1 to each vision and replace the nearest with take into account enemies linked to vision.

Edit: I would move the collision and timer to one event and then put the repeat as a subevent of it. Since you’re using object timers, you would need the same take into account condition between the collision and timer to pick the enemy1 object so you can check their timers.

Edit: wait. that doesn’t seem right. Give me a minute or 2

Final edit: you wouldn’t need the other take into account. (i have a tendency to call it among by mistake)

I don’t like doing multiple edits but I also don’t want to confuse anyone or give out bad advice.
This is what I was trying to go for with my last example. Both work but again, it’s the tiny details.

My previous example would check all of the timer values. This one only checks the relative timers.

Does the view range have any particular shape or is it just an area like a square or circle over each enemy? Because in the second case, it will be convenient for you to use instead of the collision detection of an object linked to another, use the distance of the character from the enemy. This saves you a lot of resources (collision detection when there are several objects usually consumes resources) and will make it much easier for you when you have to refer an action to the enemy that is “seeing the character”.

Q would be the character and W the enemies. You have to build your actions.

1 Like

I believe they’re rectangles. At least in an earlier version they were.

In the way I mention it is equivalent to having a circular area, just without using attached objects and collision detection.

1 Like

Agreed. A circle might be better although his enemies are square. So, IDK. a circle around a square might be weird. IDK maybe not. Everything has a center.

Hello this seemed like a nice solution!! You are right that I could delete enemy1vision but I find that now it works as intended: if the player is inside the collision area and it is also 400 pixels away(which is the area of the collision) then only that enemy shoots!!

1 Like

Fine, but if I were you, I’d remove the object that functions as a detection area and just stick with the distance calculation I mentioned. Save your project and try the way I mentioned, it will surely work exactly the same for you but without consuming so many resources. Remember that there are many ways to program something, but the less complex it is and consume fewer resources, the better.


yes, the detection squares don’t really make sense now, you’re right