How to make an enemy follow the nearest player?

I am having trouble making an enemy follow the nearest character found.

Example:

There are 4 characters, the enemy will follow whichever player is closest to them. If they come across a character that is closer than another, they follow that one.

Proximity, output first object found, with a switch I guess. Then you can have it turn back off when a certain distance away, I would do this with another(or the same) proximity but with miss going onto the on switch

I do not think this will work properly.

Oops no sorry do closest object found

{"data":{"behavior":{"v":"2","nodes":[{"gateVal":0,"mode":"greater than","inputCount":1,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter","x":720,"y":64,"group":"0","id":"0fab04c4932bb147"},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":900,"y":32,"group":"0","id":"0fab04c4b7cbb042","startVal":-2,"tag":"left"},{"delay":2,"count":0,"inputCount":3,"outputCount":1,"name":"Timer","behaviorType":"logic.triggers.Timer","x":0,"y":192,"group":"0","id":"0fab04c4317c8847"},{"expression":"A-B","default0":0,"default1":0,"default2":0,"default3":0,"default4":0,"default5":0,"params":2,"version":1,"tag":"","inputCount":3,"outputCount":1,"name":"Expression","behaviorType":"logic.logic.Expression","x":540,"y":64,"group":"0","id":"0fab04caa326664a"},{"gateVal":0,"mode":"greater than","inputCount":1,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter","x":720,"y":256,"group":"0","id":"0fab04caf7464348"},{"expression":"A-B","default0":0,"default1":0,"default2":0,"default3":0,"default4":0,"default5":0,"params":2,"version":1,"tag":"","inputCount":3,"outputCount":1,"name":"Expression","behaviorType":"logic.logic.Expression","x":540,"y":256,"group":"0","id":"0fab04cb031ea543"},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":900,"y":320,"group":"0","id":"0fab04cb5533e544","startVal":2,"tag":"up"},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":900,"y":224,"group":"0","id":"0fab04ccbea25343","startVal":-2,"tag":"down"},{"inputCount":3,"outputCount":3,"name":"Velocity","behaviorType":"logic.properties.Physics","x":1080,"y":160,"group":"0","id":"0fab04cc5cf9d64a"},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":900,"y":128,"group":"0","id":"0fab04cca7024242","startVal":2,"tag":"right"},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":180,"y":160,"group":"","id":"0fabaa784fde1c4a","_startVal":0,"dataType":2},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":360,"y":32,"group":"0","id":"0fab04cbb11cb945","targetId":0,"prop":"x"},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":360,"y":256,"group":"0","id":"0fab04cbcb63d648","targetId":0,"prop":"y"},{"inputCount":0,"outputCount":1,"name":"Always","behaviorType":"logic.triggers.Always","x":-540,"y":192,"group":"","id":"0fad713e6aec454e"},{"inputCount":2,"outputCount":3,"name":"Proximity","behaviorType":"logic.components.Prox2","x":-360,"y":192,"group":"","id":"0fad7c4b60936d42","targetEntityId":0,"threshold":150,"targetClassId":15482528,"nearestOnly":true,"firstObject":false,"allObjects":false,"contains":false,"shape":1},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":-180,"y":192,"group":"","id":"0fad98a9d7c8524b","_startVal":1,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":360,"y":128,"group":"","id":"0fae8f8b152bd143","startVal":0,"roundMode":1},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":360,"y":352,"group":"","id":"0fae90a43956b44d","startVal":0,"roundMode":1}],"links":[{"input_id":"0fab04c4932bb147i0","output_id":"0fab04caa326664ao0"},{"input_id":"0fab04c4b7cbb042i1","output_id":"0fab04c4932bb147o0"},{"input_id":"0fab04cca7024242i1","output_id":"0fab04c4932bb147o1"},{"input_id":"0fab04cc5cf9d64ai0","output_id":"0fab04c4b7cbb042o0"},{"input_id":"0fabaa784fde1c4ai2","output_id":"0fab04c4317c8847o0"},{"input_id":"0fab04caa326664ai0","output_id":"0fab04cbb11cb945o0"},{"input_id":"0fab04caa326664ai1","output_id":"0fae8f8b152bd143o0"},{"input_id":"0fab04caa326664ai2","output_id":"0fae8f8b152bd143o0"},{"input_id":"0fab04caf7464348i0","output_id":"0fab04cb031ea543o0"},{"input_id":"0fab04ccbea25343i1","output_id":"0fab04caf7464348o0"},{"input_id":"0fab04cb5533e544i1","output_id":"0fab04caf7464348o1"},{"input_id":"0fab04cb031ea543i0","output_id":"0fab04cbcb63d648o0"},{"input_id":"0fab04cb031ea543i1","output_id":"0fae90a43956b44do0"},{"input_id":"0fab04cb031ea543i2","output_id":"0fae90a43956b44do0"},{"input_id":"0fab04caf7464348i0","output_id":"0fab04cb031ea543o0"},{"input_id":"0fab04cb5533e544i1","output_id":"0fab04caf7464348o1"},{"input_id":"0fab04cc5cf9d64ai1","output_id":"0fab04cb5533e544o0"},{"input_id":"0fab04ccbea25343i1","output_id":"0fab04caf7464348o0"},{"input_id":"0fab04cc5cf9d64ai1","output_id":"0fab04ccbea25343o0"},{"input_id":"0fab04cc5cf9d64ai0","output_id":"0fab04c4b7cbb042o0"},{"input_id":"0fab04cc5cf9d64ai0","output_id":"0fab04cca7024242o0"},{"input_id":"0fab04cc5cf9d64ai1","output_id":"0fab04cb5533e544o0"},{"input_id":"0fab04cc5cf9d64ai1","output_id":"0fab04ccbea25343o0"},{"input_id":"0fab04cc5cf9d64ai0","output_id":"0fab04cca7024242o0"},{"input_id":"0fabaa784fde1c4ai1","output_id":"0fad98a9d7c8524bo0"},{"input_id":"0fabaa784fde1c4ai2","output_id":"0fab04c4317c8847o0"},{"input_id":"0fab04cbb11cb945i0","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fae8f8b152bd143i1","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fab04cbcb63d648i0","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fae90a43956b44di1","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fab04cbb11cb945i0","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fab04caa326664ai0","output_id":"0fab04cbb11cb945o0"},{"input_id":"0fab04cbcb63d648i0","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fab04cb031ea543i0","output_id":"0fab04cbcb63d648o0"},{"input_id":"0fad7c4b60936d42i1","output_id":"0fad713e6aec454eo0"},{"input_id":"0fad7c4b60936d42i1","output_id":"0fad713e6aec454eo0"},{"input_id":"0fae8f8b152bd143i0","output_id":"0fad7c4b60936d42o0"},{"input_id":"0fad98a9d7c8524bi2","output_id":"0fad7c4b60936d42o0"},{"input_id":"0fae90a43956b44di0","output_id":"0fad7c4b60936d42o1"},{"input_id":"0fad98a9d7c8524bi1","output_id":"0fad7c4b60936d42o2"},{"input_id":"0fad98a9d7c8524bi0","output_id":"0fad98a9d7c8524bo0"},{"input_id":"0fad98a9d7c8524bi1","output_id":"0fad7c4b60936d42o2"},{"input_id":"0fad98a9d7c8524bi2","output_id":"0fad7c4b60936d42o0"},{"input_id":"0fabaa784fde1c4ai1","output_id":"0fad98a9d7c8524bo0"},{"input_id":"0fad98a9d7c8524bi0","output_id":"0fad98a9d7c8524bo0"},{"input_id":"0fae8f8b152bd143i0","output_id":"0fad7c4b60936d42o0"},{"input_id":"0fae8f8b152bd143i1","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fab04caa326664ai1","output_id":"0fae8f8b152bd143o0"},{"input_id":"0fab04caa326664ai2","output_id":"0fae8f8b152bd143o0"},{"input_id":"0fae90a43956b44di0","output_id":"0fad7c4b60936d42o1"},{"input_id":"0fae90a43956b44di1","output_id":"0fabaa784fde1c4ao0"},{"input_id":"0fab04cb031ea543i1","output_id":"0fae90a43956b44do0"},{"input_id":"0fab04cb031ea543i2","output_id":"0fae90a43956b44do0"}]}}}

This should work set the proximity to the player

Would this work for only one player or more than one, I remember asking this before and the answer only applied to one player.

Yeah this only works for only one player. Also the enemy just stops moving when it isn’t in proximity of the player.

So do you want to have a randomized movement when not around the player?

No, no matter the distance, the enemy will follow the player that is closest to them.

Then increase the amount of distance

But it still only works for just one player and not more then one.

? No it should work for more… Unless you have different objects then in that case make a object called “Player Parent” or “Player DNA” and applied it to each player.

1 Like

Thank you so much. I attached a “player marker” object to each player, and put it into the proximity and it worked.

Also, is it possible to not make the enemy zig zag when they are following the player?

No Problem, nice to know I could help (:

1 Like

Never mind, I just have to change the time on the timer.

So yeah and you can do this more effectively then just change the timer. It should work the same but I asked about the same question and it should still work for you game since I used the basic enemy bundle like I did to make that code that I shared with you.

Please don’t reply on that topic since it is pretty old lol.

1 Like

Also it may not look the same since I did a lot of adjustments lol

1 Like