Advertisement
Guest User

aaa

a guest
Feb 27th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1.  
  2. if table.getn(stats.weapons) then
  3. -- Aiming Reticle
  4. game:GetService("RunService").RenderStepped:connect(function()
  5. for i, weaponName in ipairs(stats.weapons) do
  6. local pos = rayCast(parts[weaponName].barrelBreach.Position, parts[weaponName].barrelTip.Position, 4999)
  7. local aimPart = parts.weapons:FindFirstChild(weaponName):FindFirstChild("AimPart")
  8. if aimPart ~= nil then
  9. aimPart.CFrame = aimPart.CFrame:Lerp(CFrame.new(pos), .5)
  10. --aimPart.CFrame = CFrame.new(pos)
  11. end
  12. end
  13. end)
  14.  
  15. -- Turret controls
  16. while true do
  17. -- If not flipped
  18. if parts.base.Orientation.Z < 170 and parts.base.Orientation.Z > -170 then
  19. -- Table of weld updates to send to server
  20. local weldTable = {}
  21. -- Vector3 position where the mouse currently is pointing at, ignoring the vehicle model itself
  22. local mousePos = rayCast(camera.CFrame.Position , mouse.Hit.p, 4999)
  23. -- For each weapon
  24. for i, weaponName in ipairs(stats.weapons) do
  25. --- Weapon References
  26. local weapon = parts.weapons:FindFirstChild(weaponName)
  27. local barrelBreach = parts[weaponName].barrelBreach
  28. local barrelTip = parts[weaponName].barrelTip
  29. local traversalWeld = weapon:FindFirstChild("TraversalWeld")
  30. local elevationWeld = weapon:FindFirstChild("ElevationWeld")
  31. local weaponStats = stats[weaponName]
  32.  
  33. --- Aiming Variables
  34. -- Where the turret is currently aiming
  35. local currentAimPos = rayCastIgnoreAll(barrelBreach.Position, barrelTip.Position, 500);
  36. -- Where the turret needs to aim
  37. local targetAimPos = rayCastIgnoreAll(barrelBreach.Position, mousePos, 500);
  38.  
  39. ---- Traversal Calculations
  40. -- Angle of curent turret aim traversal
  41. local traversalAngle = ((- math.atan2((currentAimPos.z - parts.base.Position.z ) , (currentAimPos.x - parts.base.Position.x)) * 57.2958)) % 360
  42. -- Angle of target turret aim traversal
  43. local mouseAngle = ((- math.atan2((targetAimPos.z - parts.base.Position.z ) , (targetAimPos.x - parts.base.Position.x)) * 57.2958)) % 360
  44. -- Traversal angle difference of the barrel aim and the mouse aim
  45. local angleDif = ((mouseAngle - traversalAngle) % 360)
  46. if angleDif > 180 then
  47. angleDif = angleDif - 360
  48. end
  49.  
  50. ---- Elevation Calculations
  51. -- Verticle stud difference of the barrel aim and the mouse aim
  52. local distDif = targetAimPos.y - currentAimPos.y
  53. -- C in triangle of A B C Law of Cosines
  54. local c = (currentAimPos - Vector3.new(currentAimPos.x, targetAimPos.y, currentAimPos.z) ).magnitude
  55. -- Elevation angle difference of the barrel aim and the mouse aim
  56. local changeAngle = math.deg(math.acos((800-(c*c))/800))
  57.  
  58. -- Current Elevation and Traversal Angles relative to base
  59. local currentElevation = math.deg(elevationWeld.C0.LookVector.Y)
  60. local cfx, cfy, cfz = traversalWeld.C0:ToEulerAnglesYXZ()
  61. local currentTraversal = math.deg(cfy)
  62.  
  63. ---- Traversal Changes
  64. -- One unit of traversal
  65. if math.abs(angleDif) > (weaponStats.traversalSpeed/stats.smoothness) then
  66. -- Aim Left
  67. if angleDif > 0 and (currentTraversal < weaponStats.maxTraversal or weaponStats.maxTraversal == 180) then
  68. traversalWeld.C0 = traversalWeld.C0 * CFrame.Angles(0,math.rad(weaponStats.traversalSpeed/stats.smoothness),0)
  69. -- Aim Right
  70. elseif angleDif < 0 and ( currentTraversal > -weaponStats.maxTraversal or weaponStats.maxTraversal == 180) then
  71. traversalWeld.C0 = traversalWeld.C0 * CFrame.Angles(0,-math.rad(weaponStats.traversalSpeed/stats.smoothness),0)
  72. end
  73. -- Smaller than one unit of traversal, aim directly at aim position
  74. elseif (cfy*57.2958 < weaponStats.maxTraversal and currentTraversal > -weaponStats.maxTraversal) or weaponStats.MaxTraversal == 180 then
  75. traversalWeld.C0 = traversalWeld.C0 * CFrame.Angles(0,math.rad(angleDif),0)
  76. end
  77.  
  78. ---- Elevation Changes
  79. -- If the change needs to be at standard
  80. local elevationChange = 0
  81. -- Aim Up
  82. if distDif > 0 and currentElevation < weaponStats.maxElevation then
  83. elevationChange = -math.min( (weaponStats.elevationSpeed / stats.smoothness), (changeAngle / stats.smoothness) )
  84. -- Aim Down
  85. elseif distDif < 0 and currentElevation > weaponStats.minElevation then
  86. elevationChange = math.min( (weaponStats.elevationSpeed / stats.smoothness), (changeAngle / stats.smoothness) )
  87. end
  88. elevationWeld.C0 = elevationWeld.C0 * CFrame.Angles(0, math.rad(elevationChange),0)
  89.  
  90. ---- Inserting server updates
  91. -- Send to server new weld positions for elevation and traversal
  92. if counter == stats.smoothness then
  93. table.insert(weldTable, {elevationWeld, elevationWeld.C0})
  94. table.insert(weldTable, {traversalWeld, traversalWeld.C0})
  95. end
  96.  
  97. if weaponStats.isMain then
  98. gui.Frame.TurretHealth.Rotation = -math.deg(cfy)
  99. end
  100. end
  101. -- Send updates to server every second
  102. counter = counter + 1
  103. if counter == stats.smoothness then
  104. counter = 0
  105. event:FireServer("UpdateWelds", {weldTable} )
  106. end
  107. -- Elevation Calculation
  108. --print("Elevation Weld Angle = " .. math.deg(parts.elevationWeld.C0.LookVector.Y))
  109. -- Traversal Calculation
  110. --local cfx, cfy, cfz = parts.traversalWeld.C0:ToEulerAnglesYXZ()
  111. -- Update GUI with main gun
  112. --gui.Frame.TurretHealth.Rotation = -math.deg(cfy)
  113. counter = counter + 1
  114. wait(1.0/stats.smoothness)
  115. else
  116. -- Wait longer if flipped
  117. wait(3.0/stats.smoothness)
  118. end
  119. end
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement