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

How to make a GUI rotate towards a pixel position?

Anyone familiar with CFrame.new(position, pointAtposition)? It changes a part's position and look straight at another part. Now obviously CFrame.new doesn't work with GUIs. A few months ago the Rotation property was introduced to GuiObjects which rotates GUI's. Ok so the problem here is that I have no idea how to rotate a GUI towards a position on the screen (In pixels, not scale). I am making a game where when you click somewhere on the screen, a little GUI "plane" rotates towards it and move to the location. Also, would the rotation be entered as a normal number or with math.rad?

~Thank you

1 answer

✓ Accepted answer

Basically, you want to calculate the angle between two arbitrary points on a 2D plane.

This code will do just that:

function getAngleInDegrees(point1, point2)
	point2 = point2 - point1
	angle = math.atan2(point2.y, point2.x)
	return math.deg(angle)
end

So, say, I had an Arrow ImageLabel I wanted to point towards a Frame.

ImageLabel.Rotation = getAngleInDegrees(ImageLabel.AbsolutePositon, Frame.AbsolutePosition)

1 comment

1

It works perfectly! Thanks :D

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