Skip to content

Instantly share code, notes, and snippets.

@x4fx77x4f
Created May 28, 2023 22:54
Show Gist options
  • Save x4fx77x4f/8301800907ddce9c83210d165633627a to your computer and use it in GitHub Desktop.
Save x4fx77x4f/8301800907ddce9c83210d165633627a to your computer and use it in GitHub Desktop.
Simple freecam script for Garry's Mod.
local identifier = "x_freecam"
local self = _G[identifier]
local function unload()
local hook_tbl = self.hook_tbl
if hook_tbl ~= nil then
for hook_name, hook_name_tbl in pairs(hook_tbl) do
for hook_id, func in pairs(hook_name_tbl) do
hook.Remove(hook_name, hook_id)
end
end
end
end
if self ~= nil then
unload()
end
self = {
identifier = identifier,
unload = unload,
}
_G[identifier] = self
self.hook_tbl = {}
function self.hook_add(hook_name, hook_id, func)
if hook_id == "" then
hook_id = identifier
else
hook_id = identifier.."_"..hook_id
end
local hook_tbl = self.hook_tbl
local hook_name_tbl = hook_tbl[hook_name]
if hook_name_tbl == nil then
hook_name_tbl = {}
hook_tbl[hook_name] = hook_name_tbl
end
hook_name_tbl[hook_id] = func
return hook.Add(hook_name, hook_id, func)
end
self.enabled = false
self.origin = Vector()
self.angles = Angle()
self.hook_add("CalcView", "", function(me, origin, angles, fov, znear, zfar)
local drawviewer = false
if me == LocalPlayer() and self.enabled then
origin = self.origin
angles = self.angles
drawviewer = true
end
return {
origin = origin,
angles = angles,
fov = fov,
znear = znear,
zfar = zfar,
drawviewer = drawviewer,
}
end)
self.hook_add("CreateMove", "", function(command)
if input.WasKeyReleased(KEY_H) then
local enabled = not self.enabled
self.enabled = enabled
if not enabled then
return
end
local me = LocalPlayer()
self.origin = me:EyePos()
self.angles = me:EyeAngles()
elseif not self.enabled then
return
end
local angles = command:GetViewAngles()
self.angles = angles
local movement = Vector(
command:GetForwardMove(),
-command:GetSideMove(),
command:GetUpMove()
)
movement:Rotate(self.angles)
if command:KeyDown(IN_SPEED) then
movement:Mul(1/100)
elseif command:KeyDown(IN_WALK) then
movement:Mul(1/10000)
elseif command:KeyDown(IN_DUCK) then
movement:Mul(1/100000)
else
movement:Mul(1/1000)
end
self.origin:Add(movement)
command:ClearButtons()
command:ClearMovement()
return true
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment