Compass Math Help!

Hey everyone! I’m currently working with a compass right now and I’m struggling to get true north to point to the bearing of 0. Not sure what’s going on with the math, and I’m also pretty bad at it, but the portion of the code that sets the text of the Coordinates textfield should be displaying 0 when the compass is north, and up to 360 degrees. 360 IS 0 degrees. 360 should be true north, but instead of saying 360, it should say 0. I’m very bad with math, so I need help:

--// Compass
local look = cam.CFrame.LookVector
local look = Vector3.new(look.x, 0, look.z).unit
local lookY = math.atan2(look.z, look.x)
local difY = restrictAngle(lookY - compassLastY)
lookY = restrictAngle(compassLastY + difY*dt*compassSmoothness)
compassLastY = lookY
local angle = lookY

if angle < 0 then
	angle = angle + 7
end;

angle = math.deg(angle)

for unit, rot in pairs(units) do
	rot = restrictAngle(lookY - rot)

	if math.sin(rot) > 0 then
		local cosRot = math.cos(rot)
		local cosRot2 = cosRot*cosRot

		unit.Visible = true
		unit.Position = UDim2.new(0.5 + cosRot*0.6, unit.Position.X.Offset, 0, 3)

		unit.TextTransparency = -0.25 + 1.25 * cosRot2
		--unit.BackgroundTransparency =  0.00 + 1.50*cosRot2
		compass_Frame.Coordinates.Text = tostring(angle + 0.5 - (angle + 0.5) % 1)
	else
		unit.Visible = false
	end;
end;

Media: https://gyazo.com/8435ad311877ed9daae4b1f2fcbd46ef

1 Like