Kart

Physics · by Brixborg

Sit in it and drive with W A S D.

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

The script

-- Drives the kart. Sit in the seat, then W and S go, A and D steer.
--
-- A VehicleSeat turns the driver's keys into two numbers:
--   seat.Throttle   1 forwards, -1 backwards, 0 neither
--   seat.Steer      1 right,    -1 left,      0 neither
-- This script turns those into a push (the BodyVelocity) and a direction
-- to point in (the BodyGyro).
local chassis = script.Parent
local seat = chassis.Parent.DriverSeat
local push = chassis.BodyVelocity
local point = chassis.BodyGyro
local TURNING = 1.8   -- how sharply it steers: try 3

-- Start pointing whichever way the kart was placed.
local look = chassis.CFrame.lookVector
local heading = math.atan2(-look.X, -look.Z)
point.cframe = CFrame.Angles(0, heading, 0)

while true do
  local passed = wait()
  -- (Steering only works while moving, and is back to front in reverse.)
  heading = heading - seat.Steer * seat.Throttle * TURNING * passed
  local facing = CFrame.Angles(0, heading, 0)
  point.cframe = facing
  push.velocity = facing.lookVector * (seat.Throttle * seat.MaxSpeed)
end

More physics