Best way to detect where the gui is pointing?


I am trying to make a radial menu. I’ve made the menu items and arranged them in a circle. Then I created a gui that rotates in response to mouse movement.
this video will show you what it looks like atm

UIS.InputBegan:Connect(function(key, IsTyping)

if IsTyping then return end

if key.KeyCode == Enum.KeyCode.C then

  	oldState = player.Lock.Value
  	UIS.MouseIconEnabled = false
  BG.Visible = true
  	if player.Lock.Value == true then player.Lock.Value = false end
  Stepped = game:GetService("RunService").Stepped:Connect(function()
  	local frameCenter = frame.AbsolutePosition + (frame.AbsoluteSize/2)
  	local x = math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X)
  	frame.Rotation = math.deg(x) + 90


this is how i rotate the gui. There is only one thing to do and it’s the most dificcult one. How would i make the script recognize where the selection indicator is pointing? so that when the menu is closed that item is chosen? Any tips would be most welcome

Thank you for your time

1 Like

Since you have six items, and you already have the frame’s.Rotation, I’d say you can split up 360 degrees by 6, so that each gui has their own 60 degree intervals. And then with the rotation you already have, you figure out which item corresponds to the interval. For example:

The rotation is 27 degrees. Element 1 corresponds to interval 1 - 60, and Element 2 corresponds to interval 61 - 120. Which one does it choose? Element 1 of course, because the rotation is in between Element 1’s interval.

Ok so i made a bunch of NumberRange values. I just don’t know how i would assign these to the frame so that i can compare them with the rotation of the selector

Just order the frames by name from 1-6. I’d say create a dictionary where the indexes order from 1-6, and put in it your NumberRange values per index. You know where to go from there :smiley:

I am sorry I really don’t. I created a dictionary, however I have no clue how to assign the dictionary key to something whatever that would be, perhaps a text label?

This might be a little overcomplicated, but I believe it could possibly work. I haven’t tested it.

local choices = {} -- a table containing the selectable gui objects

table.sort(choices, function(a, b)
    local fcx, fcy = frameCenter.X, frameCenter.Y
    local absPosA , absPosB = a.AbsolutePosition, b.AbsolutePosition
    local angleA, angleB = math.atan2(absPosA.Y-fcy, absPosA.X-fcx), math.atan2(absPosB.Y-fcy, absPosB.X-fcx)
    if angleA < 0 then
        angleA += math.pi*2
    if angleB < 0 then
        angleB += math.pi*2
    return angleA < angleB

local angleBetweenChoices = math.pi/#choices
local startAngle = -angleBetweenChoices/2

local function getChoice(rotation)
    local something = rotation-startAngle -- couldn't figure out a descriptive name for this
    something = math.sign(someThing)*(math.abs(someThing)%math.pi)
    local selectedGuiObj
    if someThing < 1 then
        selectedGuiObj = choices[6-math.floor(math.abs(someThing/angleBetweenChoices))]
    else selectedGuiObj = choices[math.ceil(someThing/angleBetweenChoices)]
    return selectedGuiObj
1 Like

this is super complicated for me at least. What i ended up doing was assigning a 60° to each option and just checking if the rotation of the selector gui is within that range. Super easy, no complex angle math required. Still I’d like to thank you for taking the time to help me. I can’t even understand the math behind it i think xD

1 Like