Advertisement
RyanTV

Car script

Jul 26th, 2017
4,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. --Scripted by RyanDog2009
  2.  
  3. local car = script.Parent
  4. local stats = car.Configurations
  5. local Raycast = require(script.RaycastModule)
  6.  
  7. local mass = 0
  8.  
  9. for i, v in pairs(car:GetChildren()) do
  10. if v:IsA("BasePart") then
  11. mass = mass + (v:GetMass() * 196.2)
  12. end
  13. end
  14.  
  15. local bodyPosition = car.Chassis.BodyPosition
  16. local bodyGyro = car.Chassis.BodyGyro
  17.  
  18. --local bodyPosition = Instance.new("BodyPosition", car.Chassis)
  19. --bodyPosition.MaxForce = Vector3.new()
  20. --local bodyGyro = Instance.new("BodyGyro", car.Chassis)
  21. --bodyGyro.MaxTorque = Vector3.new()
  22.  
  23. local function UpdateThruster(thruster)
  24. -- Raycasting
  25. local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value) --game.Workspace:FindPartOnRay(ray, car)
  26. local thrusterHeight = (position - thruster.Position).magnitude
  27.  
  28. -- Wheel
  29. local wheelWeld = thruster:FindFirstChild("WheelWeld")
  30. wheelWeld.C0 = CFrame.new(0, -math.min(thrusterHeight, stats.Height.Value * 0.8) + (wheelWeld.Part1.Size.Y / 2), 0)
  31. -- Wheel turning
  32. local offset = car.Chassis.CFrame:inverse() * thruster.CFrame
  33. local speed = car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)
  34. if offset.Z < 0 then
  35. local direction = 1
  36. if speed.Z > 0 then
  37. direction = -1
  38. end
  39. wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(0, (car.Chassis.RotVelocity.Y / 2) * direction, 0)
  40. end
  41.  
  42. -- Particles
  43. if hit and thruster.Velocity.magnitude >= 5 then
  44. wheelWeld.Part1.ParticleEmitter.Enabled = true
  45. else
  46. wheelWeld.Part1.ParticleEmitter.Enabled = false
  47. end
  48. end
  49.  
  50. car.DriveSeat.Changed:connect(function(property)
  51. if property == "Occupant" then
  52. if car.DriveSeat.Occupant then
  53. car.EngineBlock.Running.Pitch = 1
  54. car.EngineBlock.Running:Play()
  55. local player = game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)
  56. if player then
  57. car.DriveSeat:SetNetworkOwner(player)
  58. local localCarScript = script.LocalCarScript:Clone()
  59. localCarScript.Parent = player.PlayerGui
  60. localCarScript.Car.Value = car
  61. localCarScript.Disabled = false
  62. end
  63. else
  64. car.EngineBlock.Running:Stop()
  65. end
  66. end
  67. end)
  68.  
  69. --spawn(function()
  70. while true do
  71. game:GetService("RunService").Stepped:wait()
  72. for i, part in pairs(car:GetChildren()) do
  73. if part.Name == "Thruster" then
  74. UpdateThruster(part)
  75. end
  76. end
  77. if car.DriveSeat.Occupant then
  78. local ratio = car.DriveSeat.Velocity.magnitude / stats.Speed.Value
  79. car.EngineBlock.Running.Pitch = 1 + ratio / 4
  80. bodyPosition.MaxForce = Vector3.new()
  81. bodyGyro.MaxTorque = Vector3.new()
  82. else
  83. local hit, position, normal = Raycast.new(car.Chassis.Position, car.Chassis.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)
  84. if hit and hit.CanCollide then
  85. bodyPosition.MaxForce = Vector3.new(mass / 5, math.huge, mass / 5)
  86. bodyPosition.Position = (CFrame.new(position, position + normal) * CFrame.new(0, 0, -stats.Height.Value + 0.5)).p
  87. bodyGyro.MaxTorque = Vector3.new(math.huge, 0, math.huge)
  88. bodyGyro.CFrame = CFrame.new(position, position + normal) * CFrame.Angles(-math.pi/2, 0, 0)
  89. else
  90. bodyPosition.MaxForce = Vector3.new()
  91. bodyGyro.MaxTorque = Vector3.new()
  92. end
  93. end
  94. end
  95. --end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement