Download

How to find the distance between player and ai?

I am part of a group creating a game where you have to survive the night in a forest filled with monsters that hunt you. I am in charge of sound. I am currently trying to create a heartbeat sound that gets louder and faster as the monster gets closer using fmod. For this to work, I need to be able to know how close the monster is to the player to determine the speed and volume of the heartbeat.

I have tried looking into using the Get Distance To node, but it is not compatible with the player controller or the ai. I have also looked into using a linetrace, but am unsure of how to go about doing that. Would that be the best method, or is there an easier way to find what I need? Also, I need to be able to only read the closest monster to the player, as there are multiple monsters and I do not want multiple heartbeats playing at once.

Thank you for any help; it is much appreciated.

There’s a few ways you can do this.

Off the player controller and/or the AI, do Get Controlled Pawn, then you can do Get Distance To.

Another tip is as long as you have the location of two vectors, subtracting them will always return the length.

So get AI controlled pawn, get player controller pawn, for each of them Get Actor Location or Get World Location, subtract the two vectors, result is the length/distance.

It returns a difference vector. You still have to “get length” from it to get a distance float value.

Controller actor doesn’t move; you have to pick the Pawn as already mentioned, get transform posistion than compare (A - B)'s Length = Distance.

Yeah just get controller 0, then drag out “get controlled pawn”, then get world location of that. If it’s a single player game you can also just “get player character”