Atan2 not working

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local origin = Vector2.new((workspace.CurrentCamera.ViewportSize.X)/2,(workspace.CurrentCamera.ViewportSize.Y)/2)


while wait do
	local pos = Vector2.new(mouse.X,mouse.Y)
	local otm = pos - origin
	local angle = math.atan2(otm.Unit.Y,otm.Unit.X)
	print(math.deg(angle))
	wait()
end

https://gyazo.com/9af8bae2f4479e78b3214fd8f37bda88
as you can see in that photo, my mouse is clearly about 45 degrees from the centre, but yet it prints its only 4 when its clearly not, it is as if the screen isnt centered properly. anyone have a solution?

I think that mouse.Y is offset by the topbar. Try adding 36 to it. Just change this line to this:

-	local pos = Vector2.new(mouse.X,mouse.Y)
+	local pos = Vector2.new(mouse.X,mouse.Y + 36)
2 Likes

It just looks like the gui frame isn’t in the same position as the origin. Try running this, it seems to work, it puts a frame at the position of the origin:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local origin = Vector2.new((workspace.CurrentCamera.ViewportSize.X)/2,(workspace.CurrentCamera.ViewportSize.Y)/2)

local sg = Instance.new("ScreenGui", player.PlayerGui)
local frame = Instance.new("Frame", sg)
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.Size = UDim2.new(0, 5, 0, 5)

while true do
    frame.Position = UDim2.new(0, origin.X, 0, origin.Y)
    local pos = Vector2.new(mouse.X,mouse.Y)
    local otm = pos - origin
    local angle = math.atan2(otm.Unit.Y,otm.Unit.X)
    print(math.deg(angle))
    wait()
end