metastruct-experiments/heat_seeking_minori.lua
2022-09-09 23:51:19 +08:00

51 lines
No EOL
1.4 KiB
Lua

-- Code by Henke and Minori
-- to remove the hook please run
-- hook.Remove("StartCommand", "autopilot")
local target = matt
SHOULD_NOCLIP = false
say("[Luadev/follow] Now following", tostring(target))
hook.Add("StartCommand", "autopilot", function(ply, cmd)
if ply ~= LocalPlayer() then return end
if not target:Alive() then return end
if target:GetPos():Distance(ply:GetPos()) < 128 then
return
end
-- we wanna run if our target is greater than 256 units
-- that way we can still catch up
if target:GetPos():Distance(ply:GetPos()) > 256 then
cmd:AddKey(IN_SPEED)
cmd:SetForwardMove(ply:GetRunSpeed())
else
cmd:SetForwardMove(ply:GetWalkSpeed())
end
-- if farther than 512 units away, noclip
if target:GetPos():Distance(ply:GetPos()) > 512 then
SHOULD_NOCLIP = true
RunConsoleCommand("noclip")
cmd:AddKey(IN_SPEED)
cmd:SetForwardMove(ply:GetRunSpeed())
end
local ang = cmd:GetViewAngles()
local targetAngle = (target:GetShootPos() - ply:GetShootPos()):GetNormalized():Angle()
targetAngle = LerpAngle(0.1, ang, targetAngle)
cmd:SetViewAngles(targetAngle)
end)
-- This hook makes sure our noclip disengages
-- Because there's no pretty way than to run this every tick
-- which is very fucking computationally expensive
-- But gmod lua is weirdge
hook.Add("Think", "noclip_think", function()
if SHOULD_NOCLIP and target:GetPos():Distance(LocalPlayer():GetPos()) < 512 then
SHOULD_NOCLIP = false
RunConsoleCommand("noclip")
end
end)