Problem with my underwater effect!

No that’s not what I want! :face_with_raised_eyebrow:
But I think the thing I send @tin_nim doesn’t match what I’m looking for tell me if I’m wrong!

THe module i send to you should be putted into a module script, And should be required and called, with the camera.CFrame.Position in a localscript, Plus the thing you said that the camera still believe its underwater, That is because you’ve got 1 fixed position for water height, That does not account for wave and such, DO you even read the link i sent to you?

1 Like

Yes it’s okay sorry I’m long to understand ! :sweat_smile:
Thank you so much ! :smiley:

But on the other hand I don’t understand how to use it to know if the camera is underwater or not, sorry I’m new! :sweat_smile:

I need to recover which variable of the module for know if I am underwater (the cam) ?

Look at the documentation of the module

--top of your code
local mod = require(pathtomodule)

--In the code you want to get the water height
local x = positionofcamerax
local z = positionofcameraz
local waterheight = mod.calcWaterHeightOffset(x, z)
1 Like

So I’ve do this, now how do i use it (sorry if I’m asking too much) ?

local RunService = game:GetService("RunService")
local mod = require(game.ReplicatedStorage.WaterHeightCalculator)

RunService.Heartbeat:Connect(function()
	local cameraPos = workspace.CurrentCamera.CFrame.Position + Vector3.new(0,2-workspace.Terrain.WaterWaveSize,0)
	local x, y, z = cameraPos.X, cameraPos.Y, cameraPos.Z
	
	local x = cameraPos.X
	local z = cameraPos.Z
	local waterheight = mod.calcWaterHeightOffset(x, z)

	local min, max = Vector3.new(x + 0.01, y + 0.01, z + 0.01), Vector3.new(x - 0.01, y - 0.01, z - 0.01)
	local region = Region3.new(max, min)
	region = region:ExpandToGrid(4)

	if region then
		local material = workspace.Terrain:ReadVoxels(region, 4)
		if material[1][1][1] == Enum.Material.Water then
			game.Lighting.Blur.Size = 10
			game.Lighting.ColorCorrection.TintColor = Color3.new(0.545098, 0.803922, 1)
		else
			game.Lighting.Blur.Size = 0
			game.Lighting.ColorCorrection.TintColor = Color3.new(1, 1, 1)
		end
	end
end)

It’s good I understand, thank you for your help it was very useful to me ! :yum:

Test it, that does work :star_struck: :

local RunService = game:GetService("RunService")
local mod = require(game.ReplicatedStorage.WaterHeightCalculator)
local Underwater = false

spawn(function()
	RunService.Heartbeat:Connect(function()
		local cameraPos = workspace.CurrentCamera.CFrame.Position + Vector3.new(0,2-workspace.Terrain.WaterWaveSize,0)
		local x, y, z = cameraPos.X, cameraPos.Y, cameraPos.Z

		local waterheight = mod.calcWaterHeightOffset(x, z)

		local min, max = Vector3.new(x + 0.01, y + 0.01, z + 0.01), Vector3.new(x - 0.01, y - 0.01, z - 0.01)
		local region = Region3.new(max, min)
		region = region:ExpandToGrid(4)
		
		if region then
			local material = workspace.Terrain:ReadVoxels(region, 4)
			if material[1][1][1] == Enum.Material.Water and waterheight > 0 or Underwater == true then
				game.Lighting.Blur.Size = 10
				game.Lighting.ColorCorrection.TintColor = Color3.new(0.545098, 0.803922, 1)
			else
				game.Lighting.Blur.Size = 0
				game.Lighting.ColorCorrection.TintColor = Color3.new(1, 1, 1)
			end
		end
	end)
end)

spawn(function()
	while wait() do
		workspace.Part.CFrame = workspace.CurrentCamera.CFrame
	end
end)

spawn(function()
	RunService.Heartbeat:Connect(function()
		local PartPos = workspace.Part.CFrame.Position + Vector3.new(0,3-workspace.Terrain.WaterWaveSize,0)
		local x, y, z = PartPos.X, PartPos.Y, PartPos.Z

		local min, max = Vector3.new(x + 0.01, y + 0.01, z + 0.01), Vector3.new(x - 0.01, y - 0.01, z - 0.01)
		local region = Region3.new(max, min)
		region = region:ExpandToGrid(4)

		if region then
			local material = workspace.Terrain:ReadVoxels(region, 4)
			if material[1][1][1] == Enum.Material.Water then
				Underwater = true
			else
				Underwater = false
			end
		end
	end)
end)