How would i create a 2d .lookAt() function

how would i create a function similar to CFrame.lookAt but for UDim2s?

heres the sort of effect i want to achieve.

image

2 Likes

You can convert the 2d direction vector into GUI.Rotation using sin and cos.

One example is pointing to the path of mouse Delta direction from the center of the screen:

3 Likes

Using rotation is a preferable way to make a GUI look at another GUI.

Can you try this
You need to pass the Gui object

local function GuiLookAt(Gui1:GuiObject,Gui2:GuiObject)
	local dist = Gui2.Position-Gui1.Position
	local vector = Vector2.new(dist.X,dist.Y).Unit
	Gui1.Rotation = math.deg(math.acos(vector.X))*math.sign(math.asin(vector.Y))
end

If you want to pass the Position instead, use this:

local function GetAngleBetween(Pos1, Pos2)
	local dist = Pos2-Pos1
	local vector = Vector2.new(dist.X,dist.Y).Unit
	return math.deg(math.acos(vector.X))*math.sign(math.asin(vector.Y))
end
1 Like

testing it now, it does actually work. I just had to change this line

local vector = Vector2.new(dist.X,dist.Y).Unit

to

local vector = Vector2.new(dist.Width.Scale, dist.Height.Scale).Unit

image

1 Like