CFrame Debugging Module

I don’t know if it isn’t posted already, but here’s a CFrame debugger module
(i yanked and modified it just a tiny bit from a wallwalking script.
Here’s the original post [Release] Custom character controller)

anyway, here’s the module

local terrain = game:GetService("Workspace").Terrain

local deb = {enabled = true}

local usedPoints = {}
local usedVectors = {}
local unusedPoints = {}
local unusedVectors = {}
local onceVector = {}

local VectorsTable = {

}

function deb.print(...)
	if (deb.enabled) then
		print(...)
	end
end

function deb.doOnce(key, func, ...)
	if not table.find(onceVector, key) then
		table.insert(onceVector, key)
		func(...)
	end
end

function deb.warn(...)
	if (deb.enabled) then
		warn(...)
	end
end

function deb.error(...)
	if (deb.enabled) then
		error(...);
	end
end

function deb.markVectos(vector, vectorName)
	if not (deb.enabled) then
		return
	end
	if not VectorsTable[vectorName] then
		VectorsTable[vectorName] = {}
		VectorsTable[vectorName].Last = nil
		VectorsTable[vectorName].Current = nil
	end
	VectorsTable[vectorName].Last = VectorsTable[vectorName].Current
	VectorsTable[vectorName].Current = vector
	deb.print(VectorsTable)
end

function deb.CFrame(cframe)
	if not deb.enabled then
		return
	end
	
	local origin = cframe.Position
	deb.vector(origin, cframe.LookVector, Color3.new(0, 0, 1))
	deb.vector(origin, cframe.UpVector, Color3.new(0, 1, 0))
	deb.vector(origin, cframe.RightVector, Color3.new(1, 0, 0))
end

function deb.point(position, colour)
	if not (deb.enabled) then
		return
	end

	local instance = table.remove(unusedPoints)

	if not instance then
		instance = Instance.new("SphereHandleAdornment")
		instance.ZIndex = 1
		instance.Name = "Debug Handle"
		instance.AlwaysOnTop = true
		instance.Radius = 0.12
		instance.Adornee = terrain
		instance.Parent = terrain
	end

	instance.CFrame = CFrame.new(position)
	instance.Color3 = colour

	table.insert(usedPoints, instance)
end

function deb.vector(position, direction, color)
	if not (deb.enabled) then
		return
	end

	local instance = table.remove(unusedVectors)

	if not instance then
		instance = Instance.new("BoxHandleAdornment")
		instance.Color3 = Color3.new(1, 1, 1)
		instance.AlwaysOnTop = true
		instance.ZIndex = 2
		instance.Transparency = 0.25
		instance.Parent = terrain
		instance.Adornee = terrain
	end

	instance.Size = Vector3.new(0.1, 0.1, direction.magnitude)
	instance.CFrame = CFrame.new(position + direction/2, position + direction)
	instance.Color3 = color

	table.insert(usedVectors, instance)
end

function deb.step()
	while (#unusedPoints > 0) do
		table.remove(unusedPoints):Destroy()
	end

	while (#unusedVectors > 0) do
		table.remove(unusedVectors):Destroy()
	end

	usedPoints, unusedPoints = unusedPoints, usedPoints
	usedVectors, unusedVectors = unusedVectors, usedVectors
end

return deb
1 Like