Motorboat

Vehicles · by Brixborg

Floats, and a script is its engine. Put it on water, sit in it, W A S D.

Use it in your games: in the studio, open View > Archive, go to the Vehicles shelf and pick "Motorboat".

The script

-- The boat's engine. Sit in the seat, then W and S go, A and D steer.
--
-- A boat has no wheels for the seat to drive, so this script reads the
-- seat's Throttle and Steer and pushes the hull itself, with two
-- BodyThrusts. (A BodyThrust is a push that turns with the part it is in,
-- like a rocket engine bolted on.)
--   Engine   pushes at the back of the boat, where the propeller is.
--            Pushing the back sideways is what turns the boat
--   Keel     stops the boat skidding sideways, like the fin under a real one
local boat = script.Parent
local hull = boat.Hull
local seat = boat.DriverSeat
local engine = hull.Engine
local keel = hull.Keel

local POWER = 70          -- how hard the engine pushes, for each 1 of the boat's weight
local RUDDER = 26         -- how hard it pushes sideways to turn
local KEEL = 3            -- how strongly sliding sideways is resisted
local PROPELLER = Vector3.new(0, -1, 7)   -- where the propeller is, on the hull

-- How heavy is the whole boat?
local weight = 0
for _, piece in ipairs(boat:GetChildren()) do
  if piece:IsA("BasePart") then weight = weight + piece:GetMass() end
end
engine.location = PROPELLER

while true do
  wait()
  -- A propeller only works under water.
  local inWater = workspace:FindWaterAt(hull.CFrame * PROPELLER) ~= nil
  if inWater then
    -- (Forwards is the hull's negative z; its right is positive x.)
    engine.force = Vector3.new(-seat.Steer * RUDDER, 0, -seat.Throttle * POWER) * weight
    local sideways = hull.CFrame:vectorToObjectSpace(hull.Velocity).X
    keel.force = Vector3.new(-sideways * KEEL * weight, 0, 0)
  else
    engine.force = Vector3.new()
    keel.force = Vector3.new()
  end
end

More vehicles