Underwater Effects

Originally made for this post: Help on a water blur script

<Script> - StarterPlayerScripts Localscript
local DetectTerrainWater = true
local UnderwaterReverb = true
local DetectPartWater = true




local TouchPart = Instance.new("Part", workspace)
TouchPart.Anchored = true
TouchPart.CanCollide = false
TouchPart.Size = Vector3.new(1, 1, 1)
TouchPart.Transparency = 1
TouchPart.Name = "TouchPart"


local Blur = Instance.new("BlurEffect", game.Lighting)
Blur.Enabled = false


local ColorCorrection = Instance.new("ColorCorrectionEffect", game.Lighting)
ColorCorrection.Enabled = false
ColorCorrection.TintColor = Color3.fromRGB(11, 143, 213)
ColorCorrection.Contrast = 0.5
ColorCorrection.Brightness = 0.4
ColorCorrection.Saturation = 0.6


local UnderwaterAmbienceSound = Instance.new("Sound", workspace.CurrentCamera)
UnderwaterAmbienceSound.Name = "UnderwaterAmbienceSound"
UnderwaterAmbienceSound.SoundId = "rbxassetid://4626145950"
if not UnderwaterAmbienceSound.IsLoaded then
	UnderwaterAmbienceSound.Loaded:Wait()
end
UnderwaterAmbienceSound.Volume = 0
UnderwaterAmbienceSound.Looped = true
UnderwaterAmbienceSound:Play()


local TouchConnection = TouchPart.Touched:Connect(function() end)


local LastReverb = nil
workspace.CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(function()
	local WaterFound = false
	if DetectTerrainWater then
		local min = workspace.CurrentCamera.CFrame.Position - (4 * Vector3.new(1, 1, 1))
		local max = workspace.CurrentCamera.CFrame.Position + (4 * Vector3.new(1, 1, 1))
		local region = Region3.new(min, max)
		local materials, occupancies = workspace.Terrain:ReadVoxels(region, 4)
		local size = materials.Size
		for x = 1, size.X, 1 do
			for y = 1, size.Y, 1 do
				for z = 1, size.Z, 1 do
					if materials[x][y][z].Name == "Water" then
						WaterFound = true
						break
					end
				end
			end
		end
	end
	if not WaterFound and DetectPartWater then
		TouchPart.CFrame = workspace.CurrentCamera.CFrame
		local TouchingParts = TouchPart:GetTouchingParts()
		for _, Part in pairs(TouchingParts) do
			if Part.Name:lower():find("water") or Part.Parent.Name:lower():find("water") then
				WaterFound = true
				break
			end
		end
	end
	if WaterFound then
		if UnderwaterReverb then
			if game.SoundService.AmbientReverb ~= Enum.ReverbType.UnderWater then LastReverb = game.SoundService.AmbientReverb end
			game.SoundService.AmbientReverb = Enum.ReverbType.UnderWater
		end
		UnderwaterAmbienceSound.Volume = 0.5
		ColorCorrection.Enabled = true
		Blur.Enabled = true
	else
		if UnderwaterReverb then
			game.SoundService.AmbientReverb = LastReverb or Enum.ReverbType.NoReverb
		end
		UnderwaterAmbienceSound.Volume = 0
		ColorCorrection.Enabled = false
		Blur.Enabled = false
	end
end)

UnderwaterEffects.rbxl (33.3 KB)

video_clip_1 (streamable.com)

36 Likes

Neat!
This is oddly specific to something I had in mind as a game feature, thanks!

1 Like

Can add sunrays too? :confused:

SunRaysEffect (roblox.com)

2 Likes

How do I decrease the blur and how do I change the color of the blur while underwater?

Example:

Before Blur:

After Blur:

image

Change the blur and color stuff here in the script

https://developer.roblox.com/en-us/api-reference/class/BlurEffect
ColorCorrectionEffect (roblox.com)

1 Like

Glitches when you die underwater. So, I made a fix that prevents that:
Relocate the script from StarterCharacter to StarterPlayer

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	UnderwaterAmbienceSound:Stop()
	ColorCorrection.Enabled = false
	Blur.Enabled = false
	game.SoundService.AmbientReverb = Enum.ReverbType.City
end)
2 Likes

It works fine with no extra changes when its in StarterPlayerScripts, I edited the post to say StarterPlayerScripts instead of StarterCharacterScripts, Idk why I had it in StarterCharacter

3 Likes