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"

How do I get the distance between player and AI

I need to find the distance between an the AIs and the player in my games. I am creating a heartbeat sound effect with fmod that will get faster as the AI gets closer to the player. Get Distance To does not work because the actor class is not compatible. I have looked into using a linetrace, but am unsure about how to get that to work. I also need to have it only play the audio for the closest AI so I do not have overlapping audio

Product Version: UE 4.14
more ▼

asked Apr 20 '17 at 03:36 PM in Blueprint Scripting

avatar image

1 1

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

1 answer: sort voted first

I'm away from the editor right now, but I think this may be a fairly solid approach. Go to your Player's blueprint. Create an array of AI Actor references called "nearbyAI". Next, create a collision circle around your main character actor. Implement the "onComponentBeginOverlap" and inside the event, cast the component to your AI Actor class. If it succeeds, FIRST check the size of the nearbyAI array.

If the length is 0, then add this actor to the list and then call "set Timer by event", and tie a new custom event to that node. Name that custom event "playHeartbeatSound". Set it to looping, with a time interval you feel is good, maybe 0.5. That means "playHeartbeatSound" will be called every .5 seconds.

However, If the length of the array is > 0, just add the AI actor to your nearbyAI array.

Inside that "playHeartbeatSound" event you just created, you'll need to do the following: First, you need to check the size of your nearbyAI array. If that is empty, then do "clearAndInvalidate" on the event so you stop this event. If the array is not empty, you'll need to loop over your array and compare the actor locations with your main character's actor location. Find the closest one and then play the sound based on the distance.

Now, you need to finally implement the "onComponentEndOverlap". Inside here, cast the component to the AI class and check to see if they are inside the nearbyAI array. If they are, then remove them, as they left your "radius of awareness".

The good thing about this approach is that once everyone leaves your radius, that looping event will stop as the nearbyAI array went to 0. And then as soon as someone comes in, that event will spin up and you'll be playing the heartbeat sounds.

This may be extremely confusing with text, and there may be a better way to blueprint this out, but if you want to, try it and see if this approach works!

more ▼

answered Apr 20 '17 at 07:34 PM

avatar image

43 2 4 7

(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