Rotate a frame of a SurfaceGui with mouse

Hey guys,

I want to rotate a Frame (Line of 2px) 360° around its own axis. To rotate the frame, you have to move the mouse around it.
The local script I wrote for it is in StarterPlayerScripts because it belongs to a system. The SurfaceGui is subordinate to the script and is set to the intended part with :Clone().

With the following script:

RS.Stepped:Connect(function()
		local Center = Line.AbsolutePosition + (Line.AbsoluteSize/2)
		local x = math.atan2(Mouse.Y - Center.Y, Mouse.X - Center.X)
		Line.Rotation = math.deg(x)
	end)

where “RS” = RunService and “Line” = the frame to rotate, I get the frame rotated a bit. However, the frame does not point to the mouse and does not rotate a full 360°.

As you can see here:
https://i.gyazo.com/981eb8943d7acd55ba1113d956b1434c.mp4

Is anyone familiar with it?

PINO

1 Like

works fine for me

Depending on the method you used to obtain the mouse position, you might have to manually add a 36 pixel offset to the Y position

local uis = game:GetService('UserInputService')
local rus = game:GetService('RunService')

rus.RenderStepped:Connect(function()
	local mPos: Vector2 = uis:GetMouseLocation()
	local center: Vector2 = dial.AbsolutePosition + dial.AbsoluteSize * .5
	local disp: Vector2 = mPos - center
	local angle: number = math.deg(math.atan2(disp.Y - 36, disp.X))
	dial.Rotation = angle + 90
end)
2 Likes

Thanks for your answer. My script also works in the ScreenGui, but I’m using a SurfaceGui here. My problem is that it just doesn’t work with the SurfaceGui. Do you have any ideas what could be causing this?

AbsolutePosition on a SurfaceGui is relative to the face of the part, not the player’s screen
You would have to use WorldToViewportPoint to get the dial’s center on the screen

2 Likes

btw here’s a demo if u need
aimAtMouseUI.rbxl (39.7 KB)

2 Likes