Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Rotation of an ImageLabel from the X and Y of the mouse?

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
script.Parent.MouseButton1Down:connect(function()
end)

This is all I really have at the moment, I am trying to minus the center of the ImageLabel (script.Parent, also my friend said to turn the center into a Vector2 but I don't really understand that) from Mouse.X and Mouse.Y and then use math.atan2 to turn it into raidans.

I am trying to do the following :

Click on a so called knob which will follow the mouse until MouseButton1 is released, so the rotation of the ImageLabel will follow the mouse, any ideas on how I can do the following I explained?

2 comments

0

all you provide us with are the mouse and the player and a useless function, this wont really count as posting your code

0

I just need some help figuring what to put in the function.

1 answer

##Math Background

Think about this from a trigonometric point of view:

tan(theta) = opp/adj

Therefore:

theta = arctan(opp/adj)

When programing, you'll want to use the function atan2 because it properly handles the divide by 0 case. So now what is opp and adj?

Think of the definition of trigonometric functions, recalling the right angle triangle. You want the angle between the x-axis and hypotenuse, so opp will be the y distance and adj will the the x distance.

To get these distances, subtract the components of the point you are starting at from the components of the point you want to point towards:

opp, adj = y2 - y1, x2 - x1

##Code Snippet

x1, y1 = gui.AbsolutePosition.X, gui.AbsolutePosition.Y
x2, y2 = mouse.X, mouse.Y
angle = math.atan2(y2 - y1, x2 - x1)

3 comments

0

Is this the full code? I am a little confused.

0

Also seems to be an error on tan(theta) = opp/adj Expected Identifier got "="

0

That isn't code, that is plain math syntax (to the best of my ability on this text editor). There is a small snippet of code at the bottom that shows some example usage for your situation to calculate the angle.

This discussion is preserved as it was. New answers, comments, and votes are closed.