How to detect if the player camera is underwater with waves?

Hey there, I’m making a game where water is quite important, so I want to add effects on the player camera whenever it is underwater. However, I’ve ran into a problem: My water has waves.

Yeah, I can detect if the camera is underwater by checking the region3, but the problem is this won’t tell me if a wave has reached the camera. My WaterWaveSize is set to 1, and I don’t want the water effects to appear before actually touching the water, and I would like them to appear when the wave reaches the camera.

Any ideas on how to do this? I’ve searched for answers but all I found was How to detect if the player camera is in water?, and it doesn’t cover waves.

I don’t think this is even possible. Thanks for reading.

2 Likes

I don’t know enough about the terrain object to know if there’s any baked method to determine height of water cells at discrete time periods. However, if I was doing it and I found that there was no documentation to support the above, I would just cast a ray from the Camera’s coordinate frame to its focus. If you hit a water cell within x tolerance distance, then a wave has now reached the camera.

One caveat, I’m unsure whether RBX truly draws those waves, or if it’s just a texture. If they don’t the suggestion above won’t work.

I’ll try that. I’ll just do a raycast to the character since it’s never meant to be underwater. I’ve got my doubts about it :confused:

I’ve tried this script, however it returns nil when the camera is underwater or the character is. Could raycasts ignore a part if the raycast is starting in it?

while true do
	wait()
	print(workspace:FindPartOnRay(Ray.new(workspace.CurrentCamera.CFrame.Position, game.Players.LocalPlayer.Character.UpperTorso.Position), game.Players.LocalPlayer.Character, false, false))
end
local shot = Camera.CFrame.lookVector * (Camera.CFrame.p - Character.Head.CFrame.p).magnitude
local ray  = Ray.new(Camera.CFrame.p, shot)

local hit, pos, surface = game.Workspace:FindPartOnRay(shot, Character, false, false)

Oh hey, that’s me!
You can check if the camera is below the current wave height in the region using this rather complicated formula: https://devforum.roblox.com/t/how-to-get-wave-height-at-a-specific-position/30672/5

Tried that, fixed some errors in the script, changed it to this:

wait(5)

while true do
	local Camera = workspace.CurrentCamera
	local Character = game.Players.LocalPlayer.Character
	local shot = Camera.CFrame.lookVector * (Camera.CFrame.p - Character.Head.CFrame.p).magnitude
	local ray  = Ray.new(Camera.CFrame.p, shot)

	print(game.Workspace:FindPartOnRay(ray, Character, false, false))
	wait()
end

But it looks like it doesn’t detect waves. It also behaves a bit weird and doesn’t always detect that it’s in water.

I’ll try that, see if it works.

If none of this works, I guess I can just block the camera from entering the water, or make the effects progressive as you start going below the allowed level.