How do I make a compass

I already have the script but I only understand like 50% of it. The code is a bit advanced but it basically makes a compass. I already know what does the trigonometry functions do like the tangent/arc tangent, cosine and sine but I don’t really know why are they used like that. For example, I don’t get why what is the point of the RestrictAngle() function, substracting angles, and adding them. Can someone explain it to me?

-- This is the code
local Smoothness = 220 / 20

local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera

local Compass = script.Parent

local LastY = 0
local Units = {
	
	[Compass:WaitForChild("North")]		= -math.pi * 1.00;
	[Compass:WaitForChild("NorthEast")] = -math.pi * 0.75;
	
	[Compass:WaitForChild("East")] 		= -math.pi * 0.50;
	[Compass:WaitForChild("SouthEast")]	= -math.pi * 0.25;
	
	[Compass:WaitForChild("South")]		=  math.pi * 0.00;
	[Compass:WaitForChild("SouthWest")]	=  math.pi * 0.25;
	
	[Compass:WaitForChild("West")]		=  math.pi * 0.50;
	[Compass:WaitForChild("NorthWest")]	=  math.pi * 0.75;
	
}

function RestrictAngle(Angle)
	if Angle < -math.pi then
		return Angle + math.pi * 2
	elseif Angle > math.pi then
		return Angle - math.pi * 2
	else
		return Angle
	end
end

while true do
	local WaitTime = wait(0.001)
	
	wait(.2)
	local Look = Camera.CoordinateFrame.LookVector
	
	local ook = Vector3.new(Look.X, 0, Look.Z).Unit
	local LookY = math.atan2(Look.Z, Look.X)
	local DifferenceY = RestrictAngle(LookY - LastY)
	LookY = RestrictAngle(LastY + DifferenceY * WaitTime * Smoothness)
	LastY = LookY

	for Unit, Rotation in pairs(Units) do
		Rotation = RestrictAngle(LookY - Rotation)
	
		if math.sin(Rotation) > 0 then
			local CosineRotation = math.cos(Rotation)
			local CosineRotation2 = CosineRotation * CosineRotation

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

			--Unit.TextTransparency = -0.25 + 1.25 * CosineRotation2
		else
			Unit.Visible = false
		end
	end
2 Likes