Faster PlaybackSpeed the closer a player is

I’m trying to figure out how to make a sound play faster the closer a player is.

local magu = (hrp.Position-player.Character.HumanoidRootPart.Position).magnitude
if magu <= 400 then
	timeel = 1 + magu
	sound.PlaybackSpeed = timeel

I know how far I am with the current formula but any ideas? I’ve had this issue before and used a pretty lame and inefficient trick to get by last time. I noticed the game Before the Dawn 2 has something like this, any ideas?

I guess a good way to do it would be lerping the playback speed. For example:

local baseSpeed = 1
local topSpeed = 5

  local magu = (hrp.Position-player.Character.HumanoidRootPart.Position).magnitude

  local percentageDistance = math.clamp( 1 - (magu - 400), 0, 1 )
  sound.PlaybackSpeed = ( baseSpeed + ( topSpeed - baseSpeed )  * percentageDistance )

the variables at the top are as follows:

  • baseSpeed is the speed that the sound will be when the player is outside the range
  • topSpeed is the speed that the sound will be when the player is at 0 distance from the target

the formula used for PlaybackSpeed is known as the lerp formula it is used to get a lerp a number between 2 given numbers in a linear fashion. The percentageDistance is how far we are away from the target in percentage.


Big brained, thanks I learned something new :muscle:

1 Like