Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Auto Aim Script with NPC and NextBot Highlighting (Using Garry's Mod Glow Effect)
- local autoAimEnabled = false
- local highlightEnabled = true
- local friendlyFireEnabled = false
- local currentTarget = nil
- local validTargets = {}
- local aimRange = 9000000
- local initializingAim = false
- local blacklist = {}
- local switchDirection = true
- local function normalizeAngle(angle)
- while angle > 180 do angle = angle - 360 end
- while angle < -180 do angle = angle + 360 end
- return angle
- end
- local function isVisible(target)
- if not IsValid(target) then return false end
- local trace = util.TraceLine({
- start = LocalPlayer():GetShootPos(),
- endpos = target:LocalToWorld(target:OBBCenter()),
- filter = LocalPlayer()
- })
- return trace.Entity == target
- end
- local function isFriendly(target)
- local friendlyClasses = { "npc_citizen", "npc_alyx" }
- if target.DrGBase and target.IsFriendlyTo and target:IsFriendlyTo(LocalPlayer()) then return true end
- return table.HasValue(friendlyClasses, target:GetClass())
- end
- local function isBlacklisted(entity)
- return blacklist[entity:GetClass()] == true
- end
- local function refreshTargets()
- validTargets = {}
- for _, entity in ipairs(ents.GetAll()) do
- if (entity:IsNPC() or entity:IsNextBot()) and isVisible(entity) then
- if not friendlyFireEnabled then
- if isBlacklisted(entity) then continue end
- if isFriendly(entity) then continue end
- end
- local distance = LocalPlayer():GetPos():DistToSqr(entity:GetPos())
- if distance <= aimRange^2 then
- table.insert(validTargets, entity)
- end
- end
- end
- end
- local function findClosestTarget()
- refreshTargets()
- if #validTargets == 0 then
- currentTarget = nil
- return
- end
- local closestDistance = math.huge
- for _, target in ipairs(validTargets) do
- local distance = LocalPlayer():GetPos():DistToSqr(target:GetPos())
- if distance < closestDistance then
- closestDistance = distance
- currentTarget = target
- end
- end
- end
- local function aimAtTarget(target)
- if not IsValid(target) then return end
- local targetPos = target:LocalToWorld(target:OBBCenter())
- local distance = LocalPlayer():GetPos():Distance(target:GetPos())
- local heightAdjustment = math.Clamp(distance / 100, 0, 10)
- targetPos.z = targetPos.z - heightAdjustment
- local directionToTarget = targetPos - LocalPlayer():GetShootPos()
- local desiredAngle = directionToTarget:Angle()
- local currentAngle = LocalPlayer():EyeAngles()
- local smoothFactor = math.Clamp(GetConVar("autoaim_smoothness"):GetFloat(), 0.01, 1)
- local yawDifference = normalizeAngle(desiredAngle.yaw - currentAngle.yaw)
- local smoothYaw = currentAngle.yaw + (yawDifference * smoothFactor)
- local pitchDifference = normalizeAngle(desiredAngle.pitch - currentAngle.pitch)
- local smoothPitch = currentAngle.pitch + (pitchDifference * smoothFactor)
- local hullHeight = target:OBBMaxs().z - target:OBBMins().z
- local dynamicMaxPitchOffset = math.Clamp(hullHeight / 20, 2, 45)
- local clampedPitch
- if initialPitchLock then
- clampedPitch = math.Clamp(smoothPitch, desiredAngle.pitch - dynamicMaxPitchOffset, desiredAngle.pitch + dynamicMaxPitchOffset)
- else
- clampedPitch = smoothPitch
- end
- local finalAngle = Angle(clampedPitch, smoothYaw, 0)
- LocalPlayer():SetEyeAngles(finalAngle)
- end
- local function initializeAimAtTarget(target)
- if not IsValid(target) then return end
- initializingAim = true
- timer.Create("InitializeAimTimer", 0.01, 100, function()
- if not IsValid(target) or not autoAimEnabled then
- initializingAim = false
- timer.Remove("InitializeAimTimer")
- return
- end
- aimAtTarget(target)
- end)
- timer.Simple(1, function()
- initialPitchLock = false
- end)
- end
- hook.Add("Think", "AutoAimControl", function()
- if autoAimEnabled and IsValid(currentTarget) then
- aimAtTarget(currentTarget)
- end
- end)
- -- Function to switch target with adjusted pitch for vertical targets
- local function switchTarget(direction)
- if not autoAimEnabled then return end
- refreshTargets() -- Refresh the valid targets list
- if #validTargets == 0 then return end
- -- Find the current target's index
- local currentIndex = table.KeyFromValue(validTargets, currentTarget) or 1
- local newIndex = (direction == "left") and ((currentIndex - 2) % #validTargets + 1)
- or ((currentIndex % #validTargets) + 1)
- -- Set the new target and reinitialize aiming
- currentTarget = validTargets[newIndex]
- initializeAimAtTarget(currentTarget)
- end
- -- Function to draw a glow around the current target
- local function drawGlow(target)
- if not IsValid(target) or not highlightEnabled then return end
- halo.Add({target}, highlightColor, 1, 1, 1, true, false)
- end
- -- Console command to enable auto aim (+autoaim)
- concommand.Add("+autoaim", function()
- autoAimEnabled = true
- findClosestTarget()
- if IsValid(currentTarget) then
- initializeAimAtTarget(currentTarget)
- end
- end)
- -- Console command to disable auto aim (-autoaim)
- concommand.Add("-autoaim", function()
- autoAimEnabled = false
- initializingAim = false
- currentTarget = nil
- timer.Remove("InitializeAimTimer")
- end)
- -- Function to switch to the previous target (left)
- concommand.Add("autoaim_switch_left", function()
- if not autoAimEnabled then return end
- refreshTargets() -- Refresh the valid targets list
- if #validTargets == 0 then return end
- -- Find the current target's index
- local currentIndex = table.KeyFromValue(validTargets, currentTarget) or 1
- -- Calculate the new index (move to the left)
- local newIndex = (currentIndex - 2) % #validTargets + 1
- -- Set the new target and reinitialize aiming
- currentTarget = validTargets[newIndex]
- initializeAimAtTarget(currentTarget) -- Restart smooth aiming
- end)
- -- Function to switch to the next target (right)
- concommand.Add("autoaim_switch_right", function()
- if not autoAimEnabled then return end
- refreshTargets() -- Refresh the valid targets listW
- if #validTargets == 0 then return end
- -- Find the current target's index
- local currentIndex = table.KeyFromValue(validTargets, currentTarget) or 0
- -- Calculate the new index (move to the right)
- local newIndex = (currentIndex % #validTargets) + 1
- -- Set the new target and reinitialize aiming
- currentTarget = validTargets[newIndex]
- initializeAimAtTarget(currentTarget) -- Restart smooth aiming
- end)
- concommand.Add("autoaim_switch", function()
- if not autoAimEnabled then return end
- refreshTargets() -- Refresh the valid targets list
- if #validTargets == 0 then return end
- -- Toggle the direction
- if switchDirection then
- RunConsoleCommand("autoaim_switch_right")
- else
- RunConsoleCommand("autoaim_switch_left")
- end
- -- Switch the direction for next time
- switchDirection = not switchDirection
- end)
- -- Console command to toggle auto-aim
- concommand.Add("autoaim_toggle", function()
- if autoAimEnabled then
- RunConsoleCommand("-autoaim")
- else
- RunConsoleCommand("+autoaim")
- end
- end)
- -- Client-side highlight color (default to cyan)
- highlightColor = {r = 0, g = 255, b = 255}
- -- Listen for the "UpdateHighlightColor" net message from the server
- net.Receive("UpdateHighlightColor", function()
- -- Read the RGB values from the server message
- local r = net.ReadUInt(8)
- local g = net.ReadUInt(8)
- local b = net.ReadUInt(8)
- -- Update the client-side highlight color
- highlightColor = {r = r, g = g, b = b}
- -- Optionally, print to the console for debugging
- print("Client received new highlight color: (" .. r .. ", " .. g .. ", " .. b .. ")")
- -- You can now use `highlightColor` in your client-side scripts, for example:
- -- Apply this color to certain client-side UI elements, highlights, or effects.
- -- Example: Change some UI elements color
- -- someUIElement:SetTextColor(Color(r, g, b))
- end)
- -- Console command to toggle friendly fire
- concommand.Add("autoaim_friendly_toggle", function()
- friendlyFireEnabled = not friendlyFireEnabled
- if friendlyFireEnabled then
- LocalPlayer():ChatPrint("Friendly fire is now enabled. Blacklist is temporarily ignored.")
- else
- LocalPlayer():ChatPrint("Friendly fire is now disabled. Blacklist is active.")
- end
- refreshTargets() -- Refresh the valid targets list after toggling friendly fire
- end)
- -- Console command to toggle an NPC class in the blacklist
- concommand.Add("autoaim_blacklist", function()
- if not IsValid(currentTarget) then
- return
- end
- local npcClass = currentTarget:GetClass()
- if blacklist[npcClass] then
- -- Remove from the blacklist if already present
- blacklist[npcClass] = nil
- LocalPlayer():ChatPrint("Removed " .. npcClass .. " from the blacklist.")
- else
- -- Add to the blacklist if not present
- blacklist[npcClass] = true
- LocalPlayer():ChatPrint("Added " .. npcClass .. " to the blacklist.")
- end
- refreshTargets() -- Make sure we refresh the targets after changing the blacklist
- end)
- -- Hook to draw the glow around the current target
- hook.Add("PreDrawHalos", "AutoAimHighlight", function()
- if IsValid(currentTarget) and highlightEnabled then
- -- Use the latest highlightColor value
- halo.Add({currentTarget}, highlightColor, 1, 1, 1, true, false)
- end
- end)
- -- Create a console variable for adjusting smoothness
- CreateConVar("autoaim_smoothness", "0.1", FCVAR_ARCHIVE, "Controls the speed of auto-aim smoothness (lower = smoother, higher = faster)")
Advertisement
Add Comment
Please, Sign In to add comment