userInputService.InputEnded:connect(function(l,c)if l.KeyCode==Enum.KeyCode.W then if movement.Y==1 then movement=Vector2.new(movement.X,0)end elseif l.KeyCode==Enum.KeyCode.A then if movement.X==-1 then movement=Vector2.new(0,movement.Y)end elseif l.KeyCode==Enum.KeyCode.S then if movement.Y==-1 then movement=Vector2.new(movement.X,0)end elseif l.KeyCode==Enum.KeyCode.D then if movement.X==1 then movement=Vector2.new(0,movement.Y)end end end)local l=0 local c=0 local g=0 for l,c in pairs(car:GetChildren())do if c:IsA("\066\097\115\101\080\097\114\116")then g=g+c:GetMass()*196.2 end end l=g*stats.Suspension.Value c=l/stats.Bounce.Value local A=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121",car.Chassis)A.velocity=Vector3.new(0,0,0)A.maxForce=Vector3.new(0,0,0)local n=Instance.new("\066\111\100\121\065\110\103\117\108\097\114\086\101\108\111\099\105\116\121",car.Chassis)n.angularvelocity=Vector3.new(0,0,0)n.maxTorque=Vector3.new(0,0,0)local Y=0 local function i(g)local A=g:FindFirstChild("\066\111\100\121\084\104\114\117\115\116")if not A then A=Instance.new("\066\111\100\121\084\104\114\117\115\116",g)end local n,i=Raycast.new(g.Position,g.CFrame:vectorToWorldSpace(Vector3.new(0,-1,0))*stats.Height.Value)local s=(i-g.Position).magnitude if n and n.CanCollide then A.force=Vector3.new(0,(stats.Height.Value-s)^2*(l/stats.Height.Value^2),0)local n=(g.CFrame:toObjectSpace(CFrame.new(g.Velocity+g.Position))).p*c A.force=A.force-Vector3.new(0,n.Y,0)else A.force=Vector3.new(0,0,0)end local T=g:FindFirstChild("\087\104\101\101\108\087\101\108\100")if T then T.C0=CFrame.new(0,-math.min(s,stats.Height.Value*.8)+T.Part1.Size.Y/2,0)local l=car.Chassis.CFrame:inverse()*g.CFrame local c=car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)if l.Z<0 then local l=1 if c.Z>0 then l=-1 end T.C0=T.C0*CFrame.Angles(0,(car.Chassis.RotVelocity.Y/2)*l,0)end T.C0=T.C0*CFrame.Angles(Y,0,0)end end

userInputService.InputEnded:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.W then
		if movement.Y == 1 then
			movement = Vector2.new(movement.X, 0)
		end
	elseif inputObject.KeyCode == Enum.KeyCode.A then
		if movement.X == -1 then
			movement = Vector2.new(0, movement.Y)
		end
	elseif inputObject.KeyCode == Enum.KeyCode.S then
		if movement.Y == -1 then
			movement = Vector2.new(movement.X, 0)
		end
	elseif inputObject.KeyCode == Enum.KeyCode.D then
		if movement.X == 1 then
			movement = Vector2.new(0, movement.Y)
		end
	end
end)

local force = 0
local damping = 0

local mass = 0

for i, v in pairs(car:GetChildren()) do
	if v:IsA("BasePart") then
		mass = mass + (v:GetMass() * 196.2)
	end
end

force = mass * stats.Suspension.Value
damping = force / stats.Bounce.Value

local bodyVelocity = Instance.new("BodyVelocity", car.Chassis)
bodyVelocity.velocity = Vector3.new(0, 0, 0)
bodyVelocity.maxForce = Vector3.new(0, 0, 0)

local bodyAngularVelocity = Instance.new("BodyAngularVelocity", car.Chassis)
bodyAngularVelocity.angularvelocity = Vector3.new(0, 0, 0)
bodyAngularVelocity.maxTorque = Vector3.new(0, 0, 0)

local rotation = 0

local function UpdateThruster(thruster)
	--Make sure we have a bodythrust to move the wheel
	local bodyThrust = thruster:FindFirstChild("BodyThrust")
	if not bodyThrust then
		bodyThrust = Instance.new("BodyThrust", thruster)
	end
	--Do some raycasting to get the height of the wheel
	local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)
	local thrusterHeight = (position - thruster.Position).magnitude
	if hit and hit.CanCollide then
		--If we're on the ground, apply some forces to push the wheel up
		bodyThrust.force = Vector3.new(0, ((stats.Height.Value - thrusterHeight)^2) * (force / stats.Height.Value^2), 0)
		local thrusterDamping = thruster.CFrame:toObjectSpace(CFrame.new(thruster.Velocity + thruster.Position)).p * damping
		bodyThrust.force = bodyThrust.force - Vector3.new(0, thrusterDamping.Y, 0)
	else
		bodyThrust.force = Vector3.new(0, 0, 0)
	end
	
	--Wheels
	local wheelWeld = thruster:FindFirstChild("WheelWeld")
	if wheelWeld then
		wheelWeld.C0 = CFrame.new(0, -math.min(thrusterHeight, stats.Height.Value * 0.8) + (wheelWeld.Part1.Size.Y / 2), 0)
		-- Wheel turning
		local offset = car.Chassis.CFrame:inverse() * thruster.CFrame
		local speed = car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)
		if offset.Z < 0 then
			local direction = 1
			if speed.Z > 0 then
				direction = -1
			end
			wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(0, (car.Chassis.RotVelocity.Y / 2) * direction, 0)
		end
		wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(rotation, 0, 0)
	end
end
