I am making some custom water that uses BodyPosition to make the player and any parts in the ‘water’ float or sink however when I remove BodyPosition from the player when they leave the ‘water’ they drop a few studs and get stuck. The parts in the water fall as expected when it is removed. This is my code - it is just in a CanCollide false part.
local keys = game:GetService("KeyboardService")
local water = script.Parent
local bodyPosition
local function onTouch(part)
if part.Parent.Parent:FindFirstChild("Humanoid") or part.Parent:FindFirstChild("Humanoid") then
local plr = part.Parent.Name
if part.Parent:FindFirstChild("HumanoidRootPart") or part.Parent.Parent:FindFirstChild("HumanoidRootPart") then
Instance.new("BodyPosition", part)
end
local waterLevel = water.Position.Y + 0.5*water.Size.Y + 0.25*part.Size.Y
local gravity = game.Workspace.Gravity
local bodyPosition = part:WaitForChild("BodyPosition")
bodyPosition.MaxForce = Vector3.new(0, gravity, 0) * 200
bodyPosition.Position = Vector3.new(0, waterLevel, 0)
bodyPosition.P = 400
bodyPosition.D = 10
else
print(part.Name)
local density = part.CustomPhysicalProperties.Density
local gravity = game.Workspace.Gravity
local topArea = part.Size.X*part.Size.Z
local volume = part.Size.X * part.Size.Y * part.Size.Z
local mass = volume*density
local weight = mass * gravity
local sink = false
local waterLevel = water.Position.Y + 0.5*water.Size.Y + 0.25*part.Size.Y
if density >= 0.9 then
sink = true
elseif density < 0.9 then
sink = false
else
warn("An error has occured")
end
if not part:FindFirstChild("BodyPosition") and sink == false then
Instance.new("BodyPosition", part)
end
if not part:FindFirstChild("BodyGyro") then
Instance.new("BodyGyro", part)
end
local bodyPosition = part:WaitForChild("BodyPosition")
bodyPosition.MaxForce = Vector3.new(0, gravity, 0) * mass * 1.6 * topArea
bodyPosition.Position = Vector3.new(0, waterLevel, 0)
bodyPosition.P = 400
bodyPosition.D = 10
end
end
local function touchEnded(part)
if part.Name ~= "Handle" then
if part.BodyPosition.Parent ~= nil then
part.BodyPosition.Parent = nil
end
end
end
water.Touched:Connect(onTouch)
water.TouchEnded:Connect(touchEnded)