Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Getting Closest Enemy to focus on

Just after some rough advice on getting distance between enemies and players, currently i have it so on perception update set the target actor as the one to move towards, my game is going to be local multiplayer with upto 4 players at a time, i am trying to get the ai to get all actors that use a specified interface, and then querying whether they are dead, im just after checking to find the closest actor to set as my focused actoralt text

I did try it like this a little while back but, as you see its overly complicated, and i had a hard time finding out whether the closest player was dead alt text

i have never touched C++ so any info/guidance via blueprints would be much appreciated

Product Version: UE 4.20
enemysight.jpg (194.6 kB)
enemysightold.jpg (216.5 kB)
more ▼

asked Nov 20 '18 at 12:42 PM in Blueprint Scripting

avatar image

6 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

below is a basic example of how you could find the closest character. in the example i used perceived actors as the potential targets but you could use whatever method you like to create the array of targets. i also did not add in the check for being dead since that could have many meanings / definitions, to check for death you would just add in another check before setting current closest.

on to how it works. the basic idea here is to loop through the list and check to see if the index we are testing is closer to the ai character (self) than the reference stored in the currentTarget variable. ok so first thing we need is a list of potential targets, used ai perception and got percieved actors to do this se we can only target things we see or hear. i then promoted the array to a variable to ensure it remains static while we run our script. next we need to set an initial value for the current closest so we get index 0 and set that as the closest, we do this so we have something valid to compare to in the next step otherwise it will try to get the distance to nothing which will always be the shortest. we are up to the loop now which is the main functionality of the script. the loop takes every index and gets its distance to the aicharacter then does the same with the current target, if the distance to the current index is less then or equal we set the current index actor to be the closest target and move on. there is also a check to ensure that the indexes being tested are not equal to 0, this is since we already set index 0 as the closest so theres no need to test it though this check may not actually be necessary. the last step is to output the closest target when the loop has completed.

alt text

capture.png (249.4 kB)
more ▼

answered Nov 20 '18 at 03:04 PM

avatar image

10.1k 33 9 23

avatar image Rand2 Feb 03 '19 at 08:39 AM

@ThompsonN13 - This was brilliant code, much appreciated! Worked like a charm :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

I created a custom blueprint node that does just that if you want to see how to do it yourself here is the link to the tutorial:


Basically you feed it any array of actors and it will sort them closest to farthest or vice versa (boolean variable you can select on the node). It outputs a sorted array with the actors and their distance away from the target actor. You can then grab the 0 index of the sorted array and that will be the closest enemy to your player.

more ▼

answered Nov 20 '18 at 06:05 PM

avatar image

Nebula Games Inc
5.4k 28 9 14

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question