Advertisement
HugePinball

markimus_rc_car.lua

Mar 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. --[[
  2. RC CAR SCRIPT BY MARKIMUS
  3. http://steamcommunity.com/id/markipie/
  4. --]]
  5.  
  6. -- Swap these values if the model you're using is moving sideways.
  7. Dir = {Forward = "z", Side = "x"}
  8.  
  9. RCCar = true
  10.  
  11. -- Changes whether the vehicle cares about touching the floor.
  12. WheelsMustTouchGround = false
  13.  
  14. AccelerationSpeed = 1.2
  15. MoveMax = 10
  16. TurnSpeed = 2.1
  17.  
  18. -- Controls which scripting buttons move the vehicle forwards, backwards, left & right.
  19. Indexes = {}
  20. Indexes[8] = "Forwards"
  21. Indexes[4] = "Left"
  22. Indexes[5] = "Backwards"
  23. Indexes[6] = "Right"
  24.  
  25. -- These should not be changed.
  26. Movement = {}
  27. Movement.Forwards = false
  28. Movement.Right = false
  29. Movement.Backwards = false
  30. Movement.Left = false
  31.  
  32. function round(n, dp)
  33.     return math.floor(n*(dp*10)+0.5)/(dp*10)
  34. end
  35.  
  36. function getGlobalForce(vector)
  37.     local local_pos = self:positionToWorld(vector)
  38.     local my_pos = self:getPosition()
  39.     local new_force = {x=local_pos.x - my_pos.x, y=local_pos.y - my_pos.y, z=local_pos.z - my_pos.z}
  40.     return new_force
  41. end
  42.  
  43. function wheelsTouchingGround()
  44.     if WheelsMustTouchGround == false then
  45.         return true
  46.     end
  47.    
  48.     local c = {}
  49.     c.Direction = {0, -1, 0}
  50.     c.type = 1
  51.     c.max_distance = 0.2
  52.     c.origin = self:getPosition()
  53.    
  54.     local results = Physics.cast(c)
  55.    
  56.     local my_pos = self:getPosition()
  57.     for _, hit in pairs(results) do
  58.         if hit.hit_object ~= self then
  59.             return true
  60.         end
  61.     end
  62.     return false
  63. end
  64.  
  65. function fixedUpdate()
  66.     if (Movement.Forwards == true or Movement.Backwards == true or Movement.Left == true or Movement.Right == true) and wheelsTouchingGround() then
  67.        
  68.         if Movement.Forwards == true or Movement.Backwards == true then
  69.             local vel = self.getVelocity()
  70.             if (math.abs(vel.x) + math.abs(vel.z)) < MoveMax then
  71.                 local add_force = {x=0, y=0, z=0}
  72.                 if Movement.Forwards == true then
  73.                     add_force[Dir.Forward] = add_force[Dir.Forward] + AccelerationSpeed
  74.                 end
  75.                 if Movement.Backwards == true then
  76.                     add_force[Dir.Forward] = add_force[Dir.Forward] - AccelerationSpeed
  77.                 end
  78.                 self:addForce(getGlobalForce(add_force), 4)
  79.             end
  80.         end
  81.        
  82.         if Movement.Left == true or Movement.Right == true then
  83.             local my_torque = {x=0, y=0, z=0}
  84.             if Movement.Left == true then
  85.                 my_torque.y = my_torque.y - TurnSpeed
  86.             end
  87.             if Movement.Right == true then
  88.                 my_torque.y = my_torque.y + TurnSpeed
  89.             end
  90.             self:setAngularVelocity(my_torque)
  91.         end
  92.     end
  93. end
  94.  
  95. function inRange(num1, num2, range)
  96.     if num1 >= num2 - range and num1 <= num2 + range then return true end
  97.     return false
  98. end
  99.  
  100. function tintSame(tint1, tint2)
  101.     local range = 0.05
  102.     if inRange(tint1.r, tint2.r, range) and inRange(tint1.g, tint2.g, range) and inRange(tint1.b, tint2.b, range) then
  103.         return true
  104.     end
  105.     return false
  106. end
  107.  
  108. function isOwner(my_col)
  109.     for _, col in pairs(getSeatedPlayers()) do
  110.         if self:getDescription() == col then
  111.             if my_col == col then
  112.                 return true
  113.             end
  114.             return false
  115.         end
  116.     end
  117.     if tintSame(stringColorToRGB(my_col), self:getColorTint()) then
  118.         return true
  119.     end
  120.     return false
  121. end
  122.  
  123. function onScriptingButtonDown(index, player_colour)
  124.     if Indexes[index] ~= nil and isOwner(player_colour) then
  125.         Movement[Indexes[index]] = true
  126.     end
  127. end
  128.  
  129. function onScriptingButtonUp(index, player_colour)
  130.     if Indexes[index] ~= nil and isOwner(player_colour) then
  131.         Movement[Indexes[index]] = false
  132.     end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement