Minimap Angle calculation help

fixed:

local Minimap = script.Parent:WaitForChild("Minimap")
local Arrow = Minimap:WaitForChild("Arrow")

local IconsFolder = game.Workspace:WaitForChild("Icons")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local Camera = Instance.new("Camera")
Camera.FieldOfView = 1 
Camera.CameraType = Enum.CameraType.Scriptable
Camera.Parent = game.Workspace
Minimap.CurrentCamera = Camera

local Ref = script.Parent.Ref 

task.wait(2)


function determineAngle(part, player)
	local directionToPart = (part.Position - player.Character.HumanoidRootPart.Position)
	local directionToPartXZ = Vector3.new(directionToPart.X, 0, directionToPart.Z).Unit
	local playerForward = player.Character.HumanoidRootPart.CFrame.LookVector
	local playerForwardXZ = Vector3.new(playerForward.X, 0, playerForward.Z).Unit
	local angle = math.deg(math.atan2(playerForwardXZ.X, playerForwardXZ.Z) - math.atan2(directionToPartXZ.X, directionToPartXZ.Z))
	angle = (angle + 360) % 360
	return math.round(angle)
end



function positionUIOnEdge(parentUI, projectedNormalizedX, projectedNormalizedY, ui)

	local padding = 0

	local clampedX = math.clamp(projectedNormalizedX, padding, 1 - padding)
	local clampedY = math.clamp(projectedNormalizedY, padding, 1 - padding)

	
	local uiAbsoluteWidth = ui.AbsoluteSize.X
	local uiAbsoluteHeight = ui.AbsoluteSize.Y
	local parentAbsoluteWidth = parentUI.AbsoluteSize.X
	local parentAbsoluteHeight = parentUI.AbsoluteSize.Y


	local uiNormalizedHalfWidth = (uiAbsoluteWidth / 2) / parentAbsoluteWidth
	local uiNormalizedHalfHeight = (uiAbsoluteHeight / 2) / parentAbsoluteHeight

	
	ui.Position = UDim2.new(
		clampedX - uiNormalizedHalfWidth, 
		0,
		clampedY - uiNormalizedHalfHeight, 
		0
	)
end


local function isPartFromCharacter(part)
	return part:IsDescendantOf(Character)
end


for i, Object in pairs(game.Workspace:GetDescendants()) do
	if isPartFromCharacter(Object) then
		continue
	end
	if Object:IsA("Terrain") or not Object:IsA("BasePart") then
		continue
	end
	Object:Clone().Parent = Minimap
end


function add()
	local ViewportSize = Minimap.CurrentCamera.ViewportSize 
	for i, child in pairs(IconsFolder:GetChildren()) do
	
		local W2VP, visible = Minimap.CurrentCamera:WorldToViewportPoint(child.Position)

	
		local normalizedX = W2VP.X / ViewportSize.X
		local normalizedY = W2VP.Y / ViewportSize.Y

		local iconGui = Ref:FindFirstChild(child.Name.."GUI")


		if visible then
		
			if iconGui then
				iconGui.Position = UDim2.new(normalizedX, 0, normalizedY, 0)
			else
		
				local TextClone = script.Parent.Parent.TextLabel:Clone()
				TextClone.Text = child.Name
				TextClone.Name = child.Name.."GUI"
				TextClone.Position = UDim2.new(normalizedX, 0, normalizedY, 0)
				TextClone.Parent = Ref
				TextClone.BackgroundTransparency = 1
				iconGui = TextClone
			end
			iconGui.Visible = true 
		else
			
			if iconGui then
				
				positionUIOnEdge(Ref, normalizedX, normalizedY, iconGui)
			else
				
				local TextClone = script.Parent.Parent.TextLabel:Clone()
				TextClone.Text = child.Name
				TextClone.Name = child.Name.."GUI"
				TextClone.Parent = Ref
				TextClone.BackgroundTransparency = 1
				iconGui = TextClone
			end
			iconGui.Visible = true 
		end
	end
end


function update()
	
	local camCFrame = CFrame.new(HumanoidRootPart.Position + Vector3.new(0, 10000, 0), HumanoidRootPart.Position)
	Camera.CFrame = camCFrame
	Arrow.Rotation = -HumanoidRootPart.Orientation.Y - 90
	add() 
end

game:GetService("RunService"):BindToRenderStep("update", 230, update)
1 Like