Making a GUI object face another

So I have some lines going from places on a weapon to GUI objects
[image]


I need the line to actually point at the bottom corner of the GUI.
This is the code I’m using right now to do what I’ve got so far:

local rotCF = CFrame.new(Vector3.new(),Vector3.new(BillboardLine.Size.X.Scale, BillboardLine.Size.Y.Scale))
local Rotation = (rotCF.lookVector.Y) * multiplier
if BillboardSetup.Direction == "Left" then Rotation = -Rotation end
LineFrame.Rotation = Rotation

LineFrame is a Frame instance within a Billboard GUI who’s sized exactly to have its bottom corner exactly on the part of the gun I want it to be on and is sized so it’s top corner is exactly on the bottom corner of the billboard GUI with the actual buttons in it. The billboardGUI that the LineFrame is in is referred to as BillboardLine.
Right now my code doesn’t really point the LineFrame where I would like it to; exactly to the corner of the BillboardGUI that it’s in.

Does anyone know of a better way of doing this?
Thanks.

3 Likes

Well I think I figured it out. Here’s my new code:

local rotCF = CFrame.new(Vector3.new(), Vector3.new(BillboardLine.Size.X.Scale, BillboardLine.Size.Y.Scale ))
local Rotation = 45 + ((rotCF.lookVector.Y - rotCF.lookVector.X ) * 45)
if BillboardSetup.Direction == "Left" then Rotation = -Rotation end
if BillboardLine.SizeOffset.Y > 0 then Rotation = -Rotation end
LineFrame.Rotation = Rotation

Now for the hard part: making the line the right length.

why not have like 1 small frame that will host the postiion start and then it wil aim at the part of the gun, then theres no aditional scripting needed.

What I’d do is

  • Use the cameras world space to screen point function, witch takes in a location in the game and returns its position on the screen in pixels. You can use this to get the parts of the gun.
  • For each of the frames, get their bottom right corner with a combination of actualPosition and actualSize (or whatever they’re called) to get the bottom right corner of each frame in pixels.
  • From there, create a GUI frame and position it to look like a line between the two points.

Sorry if that’s a bit unclear but I can’t write any code, on mobile.

Once you have the two points on your screen, math.atan2 is what you’ll need to use to get the angle. It returns radians while the property of frames is in degrees, so be sure to convert it.

2 Likes

This is what I was looking for. A simpler way to get the angle. I already know the positions and don’t know why everyone is telling me how to do that I just needed the angle.
Thanks!

1 Like

Example usage:

local Pos1 = Vector2.new(10,5)
local Pos2 = Vector2.new(30,50)
    
local Angle = math.atan2(Pos1.X - Pos2.X, Pos1.Y - Pos2.Y )
local ConvertedAngle = ((Angle / math.pi) * 180)
GUIItem.Rotation = 0 - ConvertedAngle
3 Likes