Scripting Helpers is winding down operations and is now read-only. More info→
Log in to vote

How to make a GUI rotate towards a pixel position?

Asked by
coo1o 227 Moderation Voter
10 years ago

Anyone familiar with, pointAtposition)? It changes a part's position and look straight at another part. Now obviously 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

Log in to vote
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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)

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

ImageLabel.Rotation = getAngleInDegrees(ImageLabel.AbsolutePositon, Frame.AbsolutePosition)
It works perfectly! Thanks :D coo1o 227 — 10y

Answer this question