How do I trigger function when a character's camera touches a part?
For example, I want underwater sounds to be played when the camera of a character or player is underwater, but I can't seem to script this. Pls help.
How do I trigger function when a character's camera touches a part?
For example, I want underwater sounds to be played when the camera of a character or player is underwater, but I can't seem to script this. Pls help.
Don't really know how to explain it but here's some code that shows you how to do it. It prints when the camera is inside the box and when it goes outside the box or water.
So just call the function you want called on line 24 where it prints "Camera is inside the box"
local cam = workspace.CurrentCamera
local Zones = workspace.Zones:GetChildren()
local player = game.Players.LocalPlayer
local char = player.Character
while wait() do
for q = 1, #Zones do
local cameraX = cam.CFrame.X
local cameraY = cam.CFrame.Y
local cameraZ = cam.CFrame.Z
local Hitparts = Zones[q].Hitbox:GetChildren()
for i = 1, #Hitparts do
local PartPos = Hitparts[i].Position
local PartSizeX = Hitparts[i].Size.X
local PartSizeY = Hitparts[i].Size.Y
local PartSizeZ = Hitparts[i].Size.Z
local RootPartX = char.HumanoidRootPart.CFrame.X
local RootPartY = char.HumanoidRootPart.CFrame.Y
local RootPartZ = char.HumanoidRootPart.CFrame.Z
if cameraX > PartPos.X-PartSizeX/2 and cameraX < PartPos.X+PartSizeX/2 and cameraY > PartPos.Y-PartSizeY/2 and cameraY < PartPos.Y+PartSizeY/2 and cameraZ > PartPos.Z-PartSizeZ/2 and cameraZ < PartPos.Z+PartSizeZ/2 then
if not inside then
print("Camera is inside the box")
inside = true
outside = false
end
else
if not outside then
print("Camera is outside the box.")
outside = true
inside = false
end
end
end
end
end
I think it'll be better for the sound to play when the player's head goes under the water, unless your game is first person.
This discussion is preserved as it was. New answers, comments, and votes are closed.