Hey everyone! So im trying to make a whirlpool for my new game that when a player drives a boat near it the whirlpool starts sucking the boat toward it before making the boat spin around it and get dragged under. I just cant figure it out any ideas?
local Sound = script.Parent:WaitForChild("Sound")
local damagingPart = script.Parent:WaitForChild("HitBox")
local whirlpoolCenter = script.Parent:WaitForChild("Main") -- The center part of the whirlpool
local damageAmountPerSecond = math.random(5, 15) -- Amount of damage per second
local pullForceMagnitude = 50 -- Force pulling boats towards the center
local spinForceMagnitude = 20 -- Spin force for rotating the boat
local downwardPullMagnitude = 5 -- Force pulling the boat downward
local whirlpoolDuration = 7 -- Duration in seconds the boat is affected
-- Function to apply whirlpool effects to the boat
local function applyWhirlpoolEffect(boat)
local primaryPart = boat.PrimaryPart
if not primaryPart then return end
-- Add BodyVelocity and BodyAngularVelocity for controlled physics
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(4000, 4000, 4000)
bodyVelocity.P = 1000 -- Power of the force
bodyVelocity.Parent = primaryPart
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(0, 4000, 0) -- Only rotate around Y-axis
bodyGyro.P = 3000 -- Power of the rotation
bodyGyro.Parent = primaryPart
coroutine.wrap(function()
local elapsedTime = 0
while elapsedTime < whirlpoolDuration do
-- Calculate direction to the whirlpool center
local directionToCenter = (whirlpoolCenter.Position - primaryPart.Position).unit
bodyVelocity.Velocity = directionToCenter * pullForceMagnitude + Vector3.new(0, -downwardPullMagnitude, 0)
-- Apply spinning effect
bodyGyro.CFrame = CFrame.Angles(0, math.rad(spinForceMagnitude), 0) * CFrame.new(primaryPart.Position - whirlpoolCenter.Position)
-- Apply damage over time
local takeDamageEvent = boat:FindFirstChild("TakeDamageEvent")
if takeDamageEvent and takeDamageEvent:IsA("BindableEvent") then
takeDamageEvent:Fire(damageAmountPerSecond)
end
elapsedTime = elapsedTime + 0.1
wait(0.1)
end
-- After duration, remove the forces and release the boat
bodyVelocity:Destroy()
bodyGyro:Destroy()
Sound:Play() -- Play a sound effect when the whirlpool ends
end)()
end
-- Function to detect when a boat touches the whirlpool
local function onTouch(hit)
local boat = hit.Parent:FindFirstAncestorOfClass("Model")
if boat and boat.PrimaryPart then
applyWhirlpoolEffect(boat)
end
end
damagingPart.Touched:Connect(onTouch)