Nearest enemy to my player character

not sure if i should post this here or in the main blueprint help forum

I have set up a system where as the player approaches the enemy the tempo of my music increases - its a procedural system based on a sequencer approach for the rhythm and random seeds to select the notes of the music. This is working.

I have a bunch of enemy in the level but the order that the enemy are selected is random or perhaps based on the last one I placed in the level. What I want to do is for the distance to the nearest enemy be selected. The tempo part of my blueprint is working perfectly. I hope this makes sense.

Can someone help me to refine my blueprint so the nearest enemy to the player is selected? I have tried numerous approaches with no success.

You’re checking for the distance to the “AI” actor but you arent really doing anything with it as far as I can tell (aside from comparing distance to some threshold).

What you want is to check each distance to the one before it and store that to a temporary variable if its smaller, and then get a reference to that actor and set that to another variable. Run the rest of the code at loop completion.

I was playing around with how to do this for one of my projects and I got it sort of working…

Unfortunately it’s doing something weird when there are two actors close to each other so it’s not a final solution but maybe it will point you in the right direction?

Essentially you just need to use the Get Distance To node to determine which of your enemies is closest to the player.

Thank you guys. I am going to give your suggestions a go and i will report back to you.

Yes absolutely, i am checking distance from the player character to the enemy character within a threshold of 200, that’s when the tempo of my music increases and then goes back to the normal tempo beyond that distance. I’ll add the elements you suggest. Would i place that between the calling the array and the loop node?

I forgot to mention in my original post that i am doing this in the level blueprint - i know not the best idea but coding is not my specialism so have made some errors. :slight_smile:

Had another go at it and have got a little closer to what i want to achieve. The problem now is the less than float setting, set to 400 and if another AI is over 400 the tempo remains faster.

I’m still not selecting the closest AI but any AI that is under 400 distance - at least i think that’s what is going on?

any ideas?

You should not use GetAllActorsOfClass during gameplay as it is a very slow function. Keep an array updated with the current NPCs and use that instead. GetSquaredDistanceTo is also faster than GetDistanceTo.

thank you. I am away till Friday so I will try this when I return.

Perfect, that’s fixed it. Followed what you said and its worked perfectly for me. Thank you so much for your time.

My last problem is when all the NPC’s have been killed the value returns 0.0 at which point io want the tempo to be at the slow (normal) tempo so i need to figure out how to do that.

Thanks again