Issues with my Radar System!

I really want to make a 2D Top-Down Radar System for my Horror game, but for some odd reason, whenever I move to the right it spins in circles. Same for when I move to the left or get on top of it.

This system is far from perfect but I would love to have some Insight on what might be wrong, and if you could, perfect the system for me. It’s fine if you don’t


Here’s the Images:


Here’s the Code:

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

local Part = game.Workspace:WaitForChild("Part") -- replace "Part" with the name of your part
local Gui = Player:WaitForChild("PlayerGui"):WaitForChild("Radar"):WaitForChild("Frame"):WaitForChild("Dot")

local Radius = 20 -- replace with your desired radius

while wait() do
	local RelativePosition = Part.Position - HumanoidRootPart.Position
	local Distance = RelativePosition.Magnitude
	if Distance <= Radius then
		local LookVector = Camera.CFrame.LookVector
		local Angle = math.atan2(LookVector.X, LookVector.Z) - math.atan2(RelativePosition.X, RelativePosition.Z)
		local AdjustedPosition = Vector2.new(RelativePosition.X * math.cos(Angle) - RelativePosition.Z * math.sin(Angle), RelativePosition.X * math.sin(Angle) + RelativePosition.Z * math.cos(Angle))
		Gui.Position = UDim2.new(0.5 + AdjustedPosition.X / (2 * Radius), 0, 0.5 - AdjustedPosition.Y / (2 * Radius), 0)
		Gui.Visible = true
	else
		Gui.Visible = false
	end
end

Help is much appreciated! :slight_smile: