Problem with my underwater effect!


I’m between two waves ! :sweat_smile:

I don’t understand what you mean :

:neutral_face:

I mean roblox wave height try search for it

1 Like

I have already managed this on my own but what happens is that roblox believes that have is under water while have is between two waves! :sweat_smile:

Thats not accurute enough try this Sampling Terrain Water Height at Any Position

1 Like

In the worst case test my code if you want to see my problem, increase the size of the waves and you will see that between two waves the effect is displayed ! :grinning:

I see your problem, The fact that roblox wave are sort of like noise YOu cant just use a number value to sort of approximate the height, Thats why i say you should try use this Sampling Terrain Water Height at Any Position and sample it at the player position

1 Like

Yes I understand and I looked but it won’t tell me what I want, in reality I want to get a variable that tells me when I’m between two waves that I’m not underwater and my calculation works you can try ! :wink:
Have a good day, I need to go ! :yum:
Thank you so much for your help ! :grinning:

It does not work … :disappointed_relieved:
but thanks for the help! :wink:

If we summarize my code is able to know the height where the player’s camera could be under water, that is to say the maximum height of the waves except that when the waves have passed the effect continues because the camera still believes to be under water ! :disappointed_relieved::sweat_smile:

Do the code for you???
Also @tin_nim Has already told you what to do :smile:

1 Like

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)