Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to determine if an AI can hear you?

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,

2 comments

1

My best go for this would be to fiddle with PlaybackLoudness of the players footstep sound and Distance range, although PlaybackLoudness could be completely unnecessary but that's my method if i were to ever do it.

0

Great question, man. +1 upvote.

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.