Why is Raycasting Into an Ocean Mesh Not Working?

Basically, I am trying to make a boat roll on the waves of an ocean. One method that I think will work the best is by having four ray casts on each side of the boat. Bow, Stern, Starboard, and Portside. I already have the math figured out for the orientation and position of the boat, but the problem I have is that the raycasts aren’t actually picking up waves and are instead picking up the original position of the ocean plane.

(The ocean is a meshpart that has its bones updated to create the “waves”)

bandicam 2021-07-09 16-47-16-422

The local script is in the player since the waves are also generated locally.

local RunService = game:GetService("RunService")
local Stern = game.Workspace.Boat.Stern
local Bow = game.Workspace.Boat.Bow
local wavelist = game.Workspace.Ocean.Oceans:GetDescendants()

RunService.Heartbeat:Connect(function(step)
	local sternray = Ray.new(Stern.Position, Vector3.new(0, -100, 0))
	local bowray = Ray.new(Bow.Position, Vector3.new(0, -100, 0))

	local wavefound1, position1 = workspace:FindPartOnRayWithWhitelist(sternray, wavelist)
	local wavefound2, position2 = workspace:FindPartOnRayWithWhitelist(bowray, wavelist)
	
	-- Code shows the position of the raycast as a part, all come back as the original plane position before the waves were ever generated.
    if wavefound1 then
		local Newpart = Instance.new("Part")
		Newpart.Parent = game.Workspace
		Newpart.Anchored = true
		Newpart.Position = position1

		wait(0.1)

		Newpart:Destroy()
	end
end)

If you have any solutions or ideas to solve this problem that would be awesome! Thank you!

1 Like

To my knowledge, the mesh part reacts as one whole part, as the bones don’t count towards separate parts. This means that when you’re casting your rays, technically it isn’t a syntax error, but it’s more of a logical error.

I have a few suggestions to fix this, however. First, cast multiple rays at several heights to determine once the boat is within contact of the ocean, and then cast right above that. A problem that may come out of this, however, is that the boat would work on land, so you’d have to set up some sort of counter-measure towards this.

In a second but not so helpful solution, you’d have to redesign the ocean, with multiple parts instead, as that way you’ll be able to cast the rays in the way that you are now.

I’d also like to say that the waves should probably be done on the server, as if the boats position/ rotation is based off of the waves, the boat would have to be done client sided as well, making it exploitable. The player could also alter the waves.

1 Like

Thank you for that suggestion, I took that and basically made it so a part would match the position of the bone so the ray cast hits the part not the bone. The downside to this, however, is that each ocean plane has 5000 bones :grimacing: and with 9 planes that’s a lot of parts.

Anyways, I did this and well it worked! Unfortunately, now the ship won’t move because vehicle seat is not working on Roblox’s end.

Just a suggestion here, but if you can get the nearest bone to each edge of the ship you can use that as a height reference. From there you could use a trilinear interpolation algorithm to get the height, or maybe a simple OLS regression to get the mean ship height on the water and increase or decrease the edges accordingly. Could also use a vector force to push upto the nearest bone maybe. Just some ideas

2 Likes