How would I make Gui point at another Gui?

I am currently trying to make a 2d castle defence game made up of guis, but I have run into a problem: how would I make my turret gui point in the direction of my tank guis? I have looked on Devforum but I can’t find anthing to help me.

1 Like

Atan2 math function is pretty much all you need. Could you show the layout of your gui and some script?

I don’t have any code yet or anything, but I don’t see that that matters? Anyway I all I want is for my gui to point towards another gui/udim2 point, like you can make a part look at another point using cframe. I’m not very smart when it comes to math, so could you maybe provide some sample code on how I would use atan2 for my situation?

I hope you understand, thank you!

Turns out that scaling and stuff makes the entire way of handing gui positions so dificult, but to boil it down atan2 tells us the angle of a right triangle, if you know the side lengths. The two inputs are the side lengths.
image

So if you plug in atan2(8,15) it would equal the angle a in radians. GUIs use degrees so do math.deg(angle) to make it good for guis.

Good afternoon, I don’t know if the script I made is correct, but I’ll send it here.

local turretGui = script.Parent -- assume this is the turret GUI object
local tankGui = script.TankGui -- assume this is the tank GUI object

while true do
	local turretCFrame = turretGui.CFrame
	local tankCFrame = tankGui.CFrame
	
	-- calculate the angle between the two GUI objects
	local angle = (turretCFrame.Position - tankCFrame.Position).unit:Dot(turretCFrame.LookVector)
	
	-- set the Rotation property of the turret GUI to match the angle
	turretGui.Rotation = angle
	
	wait(0.1) -- update the angle every 0.1 seconds
end

I hope it works, I just started programming. :sweat_smile:

1 Like

Thanks for trying to help :slight_smile:, but CFrame will only work with Vector3 values, and since guis use UDim2 it won’t work. I wish I did it would make my job a lot easier XD. Thanks though!

Ahh thank you, I understand atan2 now, but how exactly can I use that in my case?

The angle a tells us the orientation the gui you want. You can figure out the side lengths of the triangle by subtracting the x and y values from the both point.

1 Like

Sorry I’m not quite sure I understand, I’ll try and figure out how to do it though. Thanks, much appreciated!

You can use AbsolutePosition to get the position of the elements and subtract their coordinates to get the length and width. Then you’d use math.atan2 to get the angle in radians and convert it to degrees so you can feed it to Rotation.

local function PointTo(): ()
	local Origin: Vector2 = Frame.AbsolutePosition
	local LookAt: Vector2 = Target.AbsolutePosition
	
	local Angle: number = math.atan2(
		Origin.Y - LookAt.Y,
		Origin.X - LookAt.X
	)
	
	Frame.Rotation = math.deg(Angle)
end
1 Like

Sorry for the late reply, I only just got around to testing it. It worked though, thank you! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.