I'm making a game where and AI can find you if you make too much noise, yada yada, basically, i need a mathematical formula that decides whether or not the volume is loud enough for the AI to hear. (Louder the sound, the farther away the sound can be hear and vise versa.)
I tried Volume * Magnitude, but if the sound volume was below 1, the AI would never hear you even if you are touching it, since multiplying by a decimal goes down.
Any other formulas I could try?
Here's my code
ReplicatedStorage.MakeSound.OnServerEvent:Connect(function(Player, SoundVolume) if Chasing == false then Magnitude = (HumanoidRootPart.Position - Player.Character:FindFirstChild("HumanoidRootPart").Position).Magnitude SoundVolumeDistance = SoundVolume * Magnitude print(SoundVolumeDistance .. " Volume Distance | Volume " .. SoundVolume .. " | Magnitude " .. Magnitude) if SoundVolumeDistance >= Magnitude then print("HE HAS HEARD YOU") end end end)
The AI can hear anything louder than 1 with this script,
How I did it in one of my abandon games that I have not yet released (Probably will never). I used magnitude of the nearest players HumanoidRootPart to get there position.
local distance = 20 if ((game.Workspace.Brick.Position - game.Players.Blah.Character.HumanoidRootPart.Position).magnitude>distance) then --Do something end
Then detect if "Running" is playing in the humanoidrootpart and detect the loudness.
local loudness = 0.6 --0.5 is the default walk speed volume. 0.6 is I guess for running. You can keep this the same as even if the volume is above this number it will work. if game.Players.BLAH.Character.HumanoidRootPart.Running.Playing then game.Players.BLAH.Character.HumanoidRootPart.Running.Volume >= loudness then if game.Workspace.MonsterTargetArea == nil then return end --Make him come for you. I have made some things to help you with this to figure out the position and go even though the player is not making a sound after they moved loudly. This will only go for the current sound its attracted to. local come = game.Players.BLAH.Character.HumanoidRootPart:Clone() come.Anchored = true come.Name = "MonsterTargetArea" come.Parent = game.Workspace script.Parent:MoveTo(come) script.Parent.MoveToFinished:Wait() come:Destroy() end end