Guest User

Lock in target fix

a guest
Apr 10th, 2025
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.97 KB | None | 0 0
  1. -- Auto Aim Script with NPC and NextBot Highlighting (Using Garry's Mod Glow Effect)
  2.  
  3. local autoAimEnabled = false
  4. local highlightEnabled = true
  5. local friendlyFireEnabled = false
  6. local currentTarget = nil
  7. local validTargets = {}
  8. local aimRange = 9000000
  9. local initializingAim = false
  10. local blacklist = {}
  11. local switchDirection = true
  12.  
  13. local function normalizeAngle(angle)
  14. while angle > 180 do angle = angle - 360 end
  15. while angle < -180 do angle = angle + 360 end
  16. return angle
  17. end
  18.  
  19. local function isVisible(target)
  20. if not IsValid(target) then return false end
  21. local trace = util.TraceLine({
  22. start = LocalPlayer():GetShootPos(),
  23. endpos = target:LocalToWorld(target:OBBCenter()),
  24. filter = LocalPlayer()
  25. })
  26. return trace.Entity == target
  27. end
  28.  
  29. local function isFriendly(target)
  30. local friendlyClasses = { "npc_citizen", "npc_alyx" }
  31. if target.DrGBase and target.IsFriendlyTo and target:IsFriendlyTo(LocalPlayer()) then return true end
  32. return table.HasValue(friendlyClasses, target:GetClass())
  33. end
  34.  
  35. local function isBlacklisted(entity)
  36. return blacklist[entity:GetClass()] == true
  37. end
  38.  
  39. local function refreshTargets()
  40. validTargets = {}
  41.  
  42. for _, entity in ipairs(ents.GetAll()) do
  43. if (entity:IsNPC() or entity:IsNextBot()) and isVisible(entity) then
  44. if not friendlyFireEnabled then
  45. if isBlacklisted(entity) then continue end
  46. if isFriendly(entity) then continue end
  47. end
  48.  
  49. local distance = LocalPlayer():GetPos():DistToSqr(entity:GetPos())
  50. if distance <= aimRange^2 then
  51. table.insert(validTargets, entity)
  52. end
  53. end
  54. end
  55. end
  56.  
  57. local function findClosestTarget()
  58. refreshTargets()
  59. if #validTargets == 0 then
  60. currentTarget = nil
  61. return
  62. end
  63.  
  64. local closestDistance = math.huge
  65. for _, target in ipairs(validTargets) do
  66. local distance = LocalPlayer():GetPos():DistToSqr(target:GetPos())
  67. if distance < closestDistance then
  68. closestDistance = distance
  69. currentTarget = target
  70. end
  71. end
  72. end
  73.  
  74. local function aimAtTarget(target)
  75. if not IsValid(target) then return end
  76.  
  77. local targetPos = target:LocalToWorld(target:OBBCenter())
  78. local distance = LocalPlayer():GetPos():Distance(target:GetPos())
  79. local heightAdjustment = math.Clamp(distance / 100, 0, 10)
  80. targetPos.z = targetPos.z - heightAdjustment
  81.  
  82. local directionToTarget = targetPos - LocalPlayer():GetShootPos()
  83. local desiredAngle = directionToTarget:Angle()
  84.  
  85. local currentAngle = LocalPlayer():EyeAngles()
  86. local smoothFactor = math.Clamp(GetConVar("autoaim_smoothness"):GetFloat(), 0.01, 1)
  87.  
  88. local yawDifference = normalizeAngle(desiredAngle.yaw - currentAngle.yaw)
  89. local smoothYaw = currentAngle.yaw + (yawDifference * smoothFactor)
  90.  
  91. local pitchDifference = normalizeAngle(desiredAngle.pitch - currentAngle.pitch)
  92. local smoothPitch = currentAngle.pitch + (pitchDifference * smoothFactor)
  93.  
  94. local hullHeight = target:OBBMaxs().z - target:OBBMins().z
  95. local dynamicMaxPitchOffset = math.Clamp(hullHeight / 20, 2, 45)
  96.  
  97. local clampedPitch
  98. if initialPitchLock then
  99. clampedPitch = math.Clamp(smoothPitch, desiredAngle.pitch - dynamicMaxPitchOffset, desiredAngle.pitch + dynamicMaxPitchOffset)
  100. else
  101. clampedPitch = smoothPitch
  102. end
  103.  
  104. local finalAngle = Angle(clampedPitch, smoothYaw, 0)
  105. LocalPlayer():SetEyeAngles(finalAngle)
  106. end
  107.  
  108. local function initializeAimAtTarget(target)
  109. if not IsValid(target) then return end
  110.  
  111. initializingAim = true
  112.  
  113. timer.Create("InitializeAimTimer", 0.01, 100, function()
  114. if not IsValid(target) or not autoAimEnabled then
  115. initializingAim = false
  116. timer.Remove("InitializeAimTimer")
  117. return
  118. end
  119. aimAtTarget(target)
  120. end)
  121.  
  122. timer.Simple(1, function()
  123. initialPitchLock = false
  124. end)
  125. end
  126.  
  127. hook.Add("Think", "AutoAimControl", function()
  128. if autoAimEnabled and IsValid(currentTarget) then
  129. aimAtTarget(currentTarget)
  130. end
  131. end)
  132.  
  133.  
  134. -- Function to switch target with adjusted pitch for vertical targets
  135. local function switchTarget(direction)
  136. if not autoAimEnabled then return end
  137. refreshTargets() -- Refresh the valid targets list
  138.  
  139. if #validTargets == 0 then return end
  140.  
  141. -- Find the current target's index
  142. local currentIndex = table.KeyFromValue(validTargets, currentTarget) or 1
  143. local newIndex = (direction == "left") and ((currentIndex - 2) % #validTargets + 1)
  144. or ((currentIndex % #validTargets) + 1)
  145.  
  146. -- Set the new target and reinitialize aiming
  147. currentTarget = validTargets[newIndex]
  148. initializeAimAtTarget(currentTarget)
  149. end
  150.  
  151.  
  152. -- Function to draw a glow around the current target
  153. local function drawGlow(target)
  154. if not IsValid(target) or not highlightEnabled then return end
  155. halo.Add({target}, highlightColor, 1, 1, 1, true, false)
  156. end
  157.  
  158. -- Console command to enable auto aim (+autoaim)
  159. concommand.Add("+autoaim", function()
  160. autoAimEnabled = true
  161. findClosestTarget()
  162. if IsValid(currentTarget) then
  163. initializeAimAtTarget(currentTarget)
  164. end
  165. end)
  166.  
  167. -- Console command to disable auto aim (-autoaim)
  168. concommand.Add("-autoaim", function()
  169. autoAimEnabled = false
  170. initializingAim = false
  171. currentTarget = nil
  172. timer.Remove("InitializeAimTimer")
  173. end)
  174.  
  175.  
  176. -- Function to switch to the previous target (left)
  177. concommand.Add("autoaim_switch_left", function()
  178. if not autoAimEnabled then return end
  179. refreshTargets() -- Refresh the valid targets list
  180.  
  181. if #validTargets == 0 then return end
  182.  
  183. -- Find the current target's index
  184. local currentIndex = table.KeyFromValue(validTargets, currentTarget) or 1
  185.  
  186. -- Calculate the new index (move to the left)
  187. local newIndex = (currentIndex - 2) % #validTargets + 1
  188.  
  189. -- Set the new target and reinitialize aiming
  190. currentTarget = validTargets[newIndex]
  191. initializeAimAtTarget(currentTarget) -- Restart smooth aiming
  192. end)
  193.  
  194. -- Function to switch to the next target (right)
  195. concommand.Add("autoaim_switch_right", function()
  196. if not autoAimEnabled then return end
  197. refreshTargets() -- Refresh the valid targets listW
  198.  
  199. if #validTargets == 0 then return end
  200.  
  201. -- Find the current target's index
  202. local currentIndex = table.KeyFromValue(validTargets, currentTarget) or 0
  203.  
  204. -- Calculate the new index (move to the right)
  205. local newIndex = (currentIndex % #validTargets) + 1
  206.  
  207. -- Set the new target and reinitialize aiming
  208. currentTarget = validTargets[newIndex]
  209. initializeAimAtTarget(currentTarget) -- Restart smooth aiming
  210. end)
  211.  
  212. concommand.Add("autoaim_switch", function()
  213. if not autoAimEnabled then return end
  214. refreshTargets() -- Refresh the valid targets list
  215.  
  216. if #validTargets == 0 then return end
  217.  
  218. -- Toggle the direction
  219. if switchDirection then
  220. RunConsoleCommand("autoaim_switch_right")
  221. else
  222. RunConsoleCommand("autoaim_switch_left")
  223. end
  224.  
  225. -- Switch the direction for next time
  226. switchDirection = not switchDirection
  227. end)
  228.  
  229.  
  230.  
  231.  
  232. -- Console command to toggle auto-aim
  233. concommand.Add("autoaim_toggle", function()
  234. if autoAimEnabled then
  235. RunConsoleCommand("-autoaim")
  236. else
  237. RunConsoleCommand("+autoaim")
  238. end
  239. end)
  240.  
  241. -- Client-side highlight color (default to cyan)
  242. highlightColor = {r = 0, g = 255, b = 255}
  243.  
  244. -- Listen for the "UpdateHighlightColor" net message from the server
  245. net.Receive("UpdateHighlightColor", function()
  246. -- Read the RGB values from the server message
  247. local r = net.ReadUInt(8)
  248. local g = net.ReadUInt(8)
  249. local b = net.ReadUInt(8)
  250.  
  251. -- Update the client-side highlight color
  252. highlightColor = {r = r, g = g, b = b}
  253.  
  254. -- Optionally, print to the console for debugging
  255. print("Client received new highlight color: (" .. r .. ", " .. g .. ", " .. b .. ")")
  256.  
  257. -- You can now use `highlightColor` in your client-side scripts, for example:
  258. -- Apply this color to certain client-side UI elements, highlights, or effects.
  259. -- Example: Change some UI elements color
  260. -- someUIElement:SetTextColor(Color(r, g, b))
  261. end)
  262.  
  263.  
  264.  
  265. -- Console command to toggle friendly fire
  266. concommand.Add("autoaim_friendly_toggle", function()
  267. friendlyFireEnabled = not friendlyFireEnabled
  268. if friendlyFireEnabled then
  269. LocalPlayer():ChatPrint("Friendly fire is now enabled. Blacklist is temporarily ignored.")
  270. else
  271. LocalPlayer():ChatPrint("Friendly fire is now disabled. Blacklist is active.")
  272. end
  273.  
  274. refreshTargets() -- Refresh the valid targets list after toggling friendly fire
  275. end)
  276.  
  277.  
  278.  
  279.  
  280.  
  281. -- Console command to toggle an NPC class in the blacklist
  282. concommand.Add("autoaim_blacklist", function()
  283. if not IsValid(currentTarget) then
  284. return
  285. end
  286.  
  287. local npcClass = currentTarget:GetClass()
  288. if blacklist[npcClass] then
  289. -- Remove from the blacklist if already present
  290. blacklist[npcClass] = nil
  291. LocalPlayer():ChatPrint("Removed " .. npcClass .. " from the blacklist.")
  292. else
  293. -- Add to the blacklist if not present
  294. blacklist[npcClass] = true
  295. LocalPlayer():ChatPrint("Added " .. npcClass .. " to the blacklist.")
  296. end
  297.  
  298. refreshTargets() -- Make sure we refresh the targets after changing the blacklist
  299. end)
  300.  
  301. -- Hook to draw the glow around the current target
  302. hook.Add("PreDrawHalos", "AutoAimHighlight", function()
  303. if IsValid(currentTarget) and highlightEnabled then
  304. -- Use the latest highlightColor value
  305. halo.Add({currentTarget}, highlightColor, 1, 1, 1, true, false)
  306. end
  307. end)
  308.  
  309. -- Create a console variable for adjusting smoothness
  310. CreateConVar("autoaim_smoothness", "0.1", FCVAR_ARCHIVE, "Controls the speed of auto-aim smoothness (lower = smoother, higher = faster)")
  311.  
Advertisement
Add Comment
Please, Sign In to add comment