[SOLVED] Rotating a GUI frame or button when a player holds and rotates a gui

Hello, I am needing help rotating a GUI when a player rotates their mouse over a GUI frame or button.

How could I do this?

This is what I’ve tried so far. (It does rotate but not properly I feel like the right maths would make it work but I’m not good at maths so I’m asking for help.)

1 Like

Could you provide a visual example of what you’re trying to accomplish?

I’ll try to describe it in a very descriptive way. Basically I have a vehicle system and I’m wanting to add mobile controls so I’m wanting to make one of those on-screen steering wheels that turn when a player rotates their mouse.

You’ve most likely seen games like this off of Roblox before.

local function getAngle(mouse, frameCenter)
	return (math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X))
end

local function radToDeg(a)
	return (a * 180 / math.pi)
end

local function getRotation(frame, mouse)
	local middleFrame = frame.AbsolutePosition + (frame.AbsoluteSize / 2)
	local angle = getAngle(mouse, middleFrame)
	local degrees = radToDeg(angle)
	return degrees
end

Call GetRotation with parameters of your frame object and your mouse object, it will return a value that you can then set the frame’s rotation value to in order to make it rotate with your mouse.

3 Likes

Thank you so much!

If I were to do this by myself I wouldn’t have thought of the math.pi or math.atan2 stuff lol

Hello,

Hate to ask. But could you help me add a limit on how far it turns?

By this, I’m meaning if I turn left I want to only be able to turn 270 or fewer degrees left instead of allowing the player to rotate their mouse over and over and keep spinning. Hope this is detailed enough.

You should be able to utilize math.clamp(value, min, max) to clamp the degrees within a certain range.

I tried that but it didn’t seem to work properly. I put it where it returns the degrees. When I tried this, it got to 270 degrees then it “teleports back to 0 if I keep going around.

I did change the part that says 180 to 270 I’m not sure if I was supposed to but I’ll test it later

That’s probably because the degree returned ranges from -180 to 180, it doesn’t reach anything above 180 or below -180, so you just need to clamp it within your specific range.

Additionally if you have issues with it snapping ( as it will ) you can utilize interpolation / you can tween the rotation value rather than set it directly

Which function would I clamp it at?

Clamp the return value of getRotation.

eg:


local function getRotation(frame, mouse)
	local middleFrame = frame.AbsolutePosition + (frame.AbsoluteSize / 2)
	local angle = getAngle(mouse, middleFrame)
	local degrees = radToDeg(angle)
	return math.clamp(degrees, 10, 100) -- pseudo values
end

Yeah that’s what I did. I wanna be able to set the limit to -270 and 270 if the player keeps steering the wheel. It reaches 270 but then once I go above 270 it goes back to 0

Here’s my code and what it does so far.

When I keep going around once it reaches the 270 limit it’ll go back to 0 or -270

local Mouse = game.Players.LocalPlayer:GetMouse()
local Selected = false

local function getAngle(frameCenter)
	return (math.atan2(Mouse.Y - frameCenter.Y, Mouse.X - frameCenter.X))
end

local function radToDeg(a)
	return a * 270 / math.pi
end

local function GetRotation(frame)
	local middleFrame = frame.AbsolutePosition + (frame.AbsoluteSize / 2)
	local angle = getAngle(middleFrame)
	local degrees = radToDeg(angle)

	return math.clamp(degrees, -270, 270)
end

Steering wheel.mp4 (997.8 KB)