- What do you want to achieve?
I want to have a line that is centered at a point and follows the mouse by applying the correct size adjustment and rotation to reach the cursor.
-
What is the issue?
The actual length of the line is correct, but the rotation does not seem to be working. It either does not rotate at all in the correct place, is inverted, or does not go beyond a threshold. It seems to be stuck in only one quadrant at a time (the center assumed to be the origin) and does not pass correctly into the other three. I have found a way to get it to work for either the 1st or 4th, but not the other two.
First approach: (Works fine in the fourth quadrant, but not the others.)
Second approach: (Slight modification, now works in 1st, but not other 3.)
- What solutions have you tried so far?
As shown in the above two videos, I tried several modifications to the angle calculation logic, but none worked for all the angles. I also gave bing AI a go, but it also failed.
Here is my lua code if you are interested:
local m = game.Players.LocalPlayer:GetMouse()
local c = script.Parent.Center2
local l = c.Frame
local x1, x2, y1, y2, sqrt, abs, acos, deg = c.Position.X.Offset, 0, c.Position.Y.Offset, 0, math.sqrt, math.abs, math.acos, math.deg
while wait() do
x2, y2 = m.X, m.Y+36
l.Size = UDim2.new(0,sqrt(abs(x2-x1)^2+abs(y2-y1)^2),0,1)
--where the actual angle calculation happens. This works for 4th quadrant, but 360- the angle works for first.
c.Rotation = deg(acos(abs(x2-x1)/l.Size.X.Offset))
end
I would like to point out the following, c is a (0,1,0,1) GUI frame and is used as the anchor point so the GUI will rotate nicely. The actual line is parented to that, and so you change the center rotation, not the line’s. This is all in a standard screengui setup, I don’t think anything there is causing the problem, but rather the angle calculation code.
Thanks, let me know if you see anything that is the matter, and or how to fix it