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
asked Apr 20 '17 at 03:36 PM in Blueprint Scripting
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!
answered Apr 20 '17 at 07:34 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here