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.
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’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
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.