Move to location of enemy in array

Hi all, would anyone be able to quickly show me how to get an array of surrounding enemies and move my character to the location of each enemy starting with the closest one? I’m messing with some array stuff rn but not sure I’m understanding it all properly .

You’ll need to be a bit more specific with what you are not understanding.

Did this quick test sorting based on distance. It’s really not that complicated, but it can be a bit hard to explain not knowing what you are having trouble with.

Sorry for the lack of info. what I’m trying to do is get all enemies around my character then with a button press the player teleports to the closest enemy ,plays attack montage,moves to the next enemy with another button press and so on. Ive managed to detect all surrounding enemies with a collision sphere using ‘get overlapping actors’ . I’m just not sure how I tell my character in what order he’s supposed to teleport. I’m Sorry if this seems really noob. I am watching youtube videos trying to piece together how this would all work
Thanks again

Just for reference, I imagined something close to this.

You need to get those actors and add a reference to an array: The way I did it in the link above is they are constantly added and removed as they overlap with the character just to save myself an extra loop.

In this case I used a map container, but just use a normal array if you are not familiar.

You then need to loop through that reference array and store the distance between each against the character in a float array:

Finally, since the float and reference array are in same order: use Min Of Float Array to loop through the floats and find the index with the lowest value, in this case the lowest distance. Take that index and get the location of that reference from the other array and teleport:

After the teleport you could call another event with what ever logic you want (delays, animations, etc). Just keep in mind that you will always teleport to the last one you teleported unless you remove if from the array or player moves closer to another valid actor.

After you have it working with teleport, try moving the player to your target in X amount of time depending on distance. Should be a fun exercise.

Hope this helps!

1 Like

Wow! Thanks so much. I’m about to play with this right now

1 Like

So… this was actually something I adapted on the go and was not really happy with it… so I made a different version. :roll_eyes:Sort based on distance - YouTube

It outputs an array with a custom size in order. Could work to teleport through X amount of targets in X amount of time.

Had fun creating this. If you’re interested: Sort based on distance posted by pezzott1 | blueprintUE | PasteBin For Unreal Engine 4

Feel free to share what you ended up doing or if you have any questions.

1 Like

Thanks for this. I’ll definitely have a go at it

So ive managed to get my character moving through my array of enemies on a button press per enemy. Now how do I go about doing it on a single button press ? For loop or maybe timer by function or event? I’m really not sure . Maybe I need to watch more vids. Also, I used ‘move component to’ in place of the teleport. Move component to node gave me a float pin “over time”. so I divided min of float array by the distance from enemy to the player(get distance to) and plugged the result into the move component node. Idk if it’s the correct or best way to do it but my character does move faster when the target is further away

You could set a custom event with a timer (not a delay, this timer) that on every fire it moves the player to next actor in array. you’ll need to keep count of how many times it fires so when it equals the length of the array -1 you disable the timer.

Or do something like this with a bit of vector math.

BTW updated the link above with the blueprint. Should work a fraction faster now… :sweat_smile:

1 Like

Ok. Sounds like a piece of cake :+1::laughing:
I’ll give it a try

1 Like