What do you want to achieve? I am trying to make a ship float on my custom water (made using mesh deformation) I’m trying to make it rock back, forwards and side to side. I’ve seen people use raycasting for this but I struggle doing this.
What is the issue? I can’t get boats to float on my custom water. Here are some scripts that I have tried already:
local bow = script.Parent.Bowray
local stern = script.Parent.Sternray
local wavelist = workspace.Ocean2.Plane
-- Set an origin and directional vector
local bowray = bow.Position
local sternray = stern.Position
local rayDirection = Vector3.new(0, -100, 0)
-- Build a "RaycastParams" object and cast the ray
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {bowray.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
local raycastParams2 = RaycastParams.new()
raycastParams2.FilterDescendantsInstances = {sternray.Parent}
raycastParams2.FilterType = Enum.RaycastFilterType.Whitelist
local bowrayresult = workspace:Raycast(bowray, rayDirection, raycastParams)
local sternrayresult = workspace:Raycast(sternray, rayDirection, raycastParams)
local part2, pos2 = workspace:FindPartOnRayWithWhitelist(sternrayresult, wavelist)
local part, pos = workspace:FindPartOnRayWithWhitelist(bowrayresult, wavelist)
if part or part2 then
script.Parent.CFrame = CFrame.new(pos, pos2)
end
What solutions have you tried so far? I’ve been scanning around the developer forum but have had no luck!
You cant use raycasting because mesh deformation doesnt interact with raycasts too accurately. You need to instead sample the height of water at various points along the ship’s hull and calculating their bouyant force. From there you can compare it against the weight and displacement to get a final force acting on the ship, then combine all the sample points against the center of mass to get an angle and position of the ship.
To be honest the other reply would probably work, but I have tried the buoyancy method, and it’s extremely laggy and buggy, instead I would reccomend using maybe 3-4 body positions along the ship with a Y force, and change their height according to the wave.
For the volume (Vs) you use the volume of the ship thats below the waterline. D is the density, in this case salty water is 1023.6 kg/m³, and g is gravity which can be workspace.Gravity or 198.2.
If they body position are in different spots this shouldn’t happen unless: the MaxForce isn’t set to (0, n, 0) or the difference between the two heights is more than the distance between the two body positions.
Using a few BodyPositions is probably the easiest way.
Edit:
RatiusRat is correct, I thought there was a ApplyAtCenter property for BodyPositions. The BodyPositions have to affect separate assemblies then, so you’d need to use BallSocketConstraints or hinges–anything that makes a new assembly and connects it to the larger one.
A better way is to calculate the points, find the orientation and position relative to the center of mass, then use a BodyPosition and BodyGyro to position the boat.
I created this version myself, I saw a post on the dev forum and was inspired by it. I wanted to try it myself. I’m not super experienced in coding, however I managed to create this using a gerstner wave pattern.
Have you tested that theory though? I’ve welded two parts together and both have a bodyposition. They are set at different positions yet only 1 is active while the other is cancelled.