I need help with ray casting water

Hello, I made water using the terrain editor and I put a position let’s call A. I made a ray with the starting position called A. Position and the direction is Vector3.new(0,-1,0). Position A is on top of the water but when I print ray it says nil. I don’t understand what I’m doing wrong.

local positionOverWater = Vector3.new(0,100,0) -- This is an example. Let's say the top of the water is Vector3.new(0,90,0). It doesn't print anything.

local direction = Vector3.new(0,-1,0)

local params = RaycastParams.new()

params.FilterType = Enum.RaycastFilterType.Whitelist

params.IgnoreWater = false

params.FilterDescendantsInstances = {workspace.Terrain,workspace.Baseplate}

local ray = workspace:Raycast(positionOverWater,direction,params)


Alright I solved this issue:

-- Let's say the top of the water is Vector3.new(0,90,0). How would I detect it?

-- First, make the startingPosition something like 10 studs higher than the top of the water.
-- Second, make the endingPosition something like 80 studs lower than the top of the water.
-- The raycast will start at the startingPosition, then go down to the endingPosition.
-- ALWAYS remember, the first argument is the startingPosition, the second argument is the endingPosition - startingPosition.
-- This is the formula needed for the ray to go from the startingPosition to the endingPosition.

local startingPosition = Vector3.new(0,100,0)

local endingPosition = Vector3.new(0,10,0)

local ray = workspace:Raycast(startingPosition, endingPosition - startingPosition)


Ima leave this here in case someone needs it for the future. Basically do the ending position minus the starting position.

1 Like