x

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. 

Set Actor Closest To Player?

Hello I'd like to get the closest actor from my player and set it as a variable, I used get all actors of class along with a for each loop and compared the distance from the actor to my character using the get distance to node but it only seems to set only one of the actors and doesn't update to the next closest actor.

I assume I'm using for each loop wrong or something. Any help would be appreciated thanks!

Product Version: UE 4.21
Tags:
more ▼

asked May 16 '19 at 10:48 PM in Blueprint Scripting

avatar image

Jinkuu
73 2 3 6

avatar image Everynone May 16 '19 at 11:13 PM

it only seems to set only one of the actors and doesn't update to the next closest actor.

So what do you need? The closest actor or the 2nd closest? Or it does not work at all?

avatar image Jinkuu May 17 '19 at 01:09 AM

I'd like it to update the variable when another actor becomes the closest currently it will set whichever one is the closest first then it wont change after that even when that actor moves farther away from the player

I need the closest actor.

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

1 answer: sort voted first

I'd like it to update the variable when another actor becomes the closest currently it will set whichever one is the closest first then it wont change after that even when that actor moves farther away from the player

I need the closest actor.

Thanks for clarification, this should do the trick:

alt text

To make it more efficient you could have an overlapping sphere that checks only for actors in proximity. Checking against actors that are far, far away may not be necessary - although this would depend on how your game is structured, of course, and may make perfect sense.

If you have a fixed amount of actors to check against, it would also make sense to gather them in an array only once and then perform periodic checks on the results.

Image from Gyazo

capture.png (225.1 kB)
more ▼

answered May 17 '19 at 08:12 AM

avatar image

Everynone
14.6k 76 25 61

avatar image Jinkuu May 17 '19 at 12:38 PM

Thank you!

(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