[SOLVED] Help with limiting GUI Rotation

Hello, I am trying to make a steering wheel that shows on mobile players screens when their driving cars. I am wanting to make it so the player can turn the steering wheel in between the numbers -270 and 270 but, when the player gets to 270 it’ll automatically goes back to 0 I don’t want it to do anything if the player keeps going.

Here’s what I got so far:

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

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

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

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)

Shouldn’t it be 360? I think that’s what’s causing the issue, buuuuuuttt

You can just use math.deg, it will be faster most of the time;

local degrees = math.deg(angle)

Also you could probably rename angle to radian because that’s technically the correct name.

1 Like

I tried that earlier and it didn’t work.

Can you send the code you have right now after the changes I told you to make?

Alright, I’ll send it soon when I go on my laptop

Just to make things easier, here’s a place file for it.

Steering wheel test.rbxl (33.0 KB)

I would have preferred if you just pasted your code into a reply, but anyway…

The issue, now that I look at it more closely, is that rotation goes from -180 to 180, a full 360 degrees, it automatically snaps to -180 if the rotation is 185 for example.

So, the correct clamping values would actually be:

local RotationClamped = math.clamp(math.deg(Radian), -135, 135)

, because 270 divided by 2 is 135, and now it works fine.

(you would probably also want to introduce some maximum angle difference limit, because the snapping is quite obvious if you move your mouse the full circle)

Okay, thanks!

How would I do that?

I’m bad at maths by the way.

You could change the part of the code to this;

--how much the wheel can turn at once
--anything higher than this and the wheel will not turn

local LastRotation

	if Selected == true then
		local NewRotation = GetRotation(script.Parent.MainGui.SteeringWheel)
		--math.abs converts a negative number to a positive number
		--but does nothing to positive numbers
		if not LastRotation or math.abs(NewRotation - LastRotation) <= MAX_MOVE_PER_UPDATE then
			script.Parent.MainGui.SteeringWheel.Rotation = NewRotation
			LastRotation = NewRotation

Okay, thanks!

I’ll let you know how it goes when I test it.

Thanks a lot!

It works well!

I should be good with everything else I want to do to it.

1 Like