Getting BillboardGui to follow with the mouse

At the moment I have 4 buttons, but looking at the green one in particular. When you click and hold this button down it allows you to move an object, and the other 3 buttons disappear. Now, I want this button to follow the mouse, which I thought since it’s in a BillboardGui would mean once you clicked the button and move the model around, the button would stay in the right spot, however, it’s not.

Click and move the item

The button isn’t in the same position of the mouse.

There’s no code involved with position the buttons. The code just moves the model, which in turns move the billboard ui

1 Like

I think the reason this is happening is because the green button is not over the same position as the center of the item your trying to move. The button is offset from the model. So when you move your mouse to a cetain position, the PrimaryPart or the center of the model (whichever you are doing) is being moved to the mouse and since the button is offset from the center or PrimaryPart of the model it is no longer in the same position as the mouse. I hope that makes sense.

The easiest solution is to move the green button so that its hovering over the center of the model if your using MoveTo() or make it hover over the PrimaryPart if your using SetPrimaryPartCFrame().

1 Like

How can I get UDim2 from CFrame??

Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ) * CFrame.Angles(0, math.rad(Rotation), 0))
MoveButton.Position = Model.PrimaryPart.CFrame

This is a little edgy cuz there is no concrete way to get the screen position of a billboard gui but this might work. Btw this code assumes the BillboardGui centered on its Adornee

local camera = workspace.CurrentCamera
local billboard = --whereever your billboard gui is
local billboardCenter = camera:WorldToViewportPoint(billboard.Adornee)
local billboardTopLeft = Vector2.new(billboardCenter.X - billboard.Size.X.Offset, billboardCenter.Y - billboard.Size.Y.Offset)
local primaryPartXY = camera:WorldToViewportPoint(Model.PrimaryPart.Position)

local buttonPosition = UDim2.new(0, primaryPartXY.X - billboardTopLeft.X, 0, primaryPartXY.Y - billboardTopLeft.Y)
MoveButton.Position = buttonPosition

I think this will work if you want to move the button during the game. If you dont want to do this then you could just have the button permanantly in a place where it would always hover over the PrimaryPart. Hope this helps

[ Unable to cast Instance to Vector3]

local camera = workspace.CurrentCamera
local billboard = script.Parent
local billboardCenter = camera:WorldToViewportPoint(billboard.Adornee) -- ERROR HERE Adorned to a model
local billboardTopLeft = Vector2.new(billboardCenter.X - billboard.Size.X.Offset, billboardCenter.Y - billboard.Size.Y.Offset)
local primaryPartXY = camera:WorldToViewportPoint(Model.PrimaryPart.Position)

local buttonPosition = UDim2.new(0, primaryPartXY.X - billboardTopLeft.X, 0, primaryPartXY.Y - billboardTopLeft.Y)
MoveButton.Position = buttonPosition

OH whoops sorry change billboard.Adornee to

billboard.Adornee.Position
local HUD = script.Parent -- BillboardGui

local Model = HUD.Adornee

local BillboardCenter = Camera:WorldToViewportPoint(Model.PrimaryPart.Position)
local BillboardTopLeft = Vector2.new(BillboardCenter.X - HUD.Size.X.Offset, BillboardCenter.Y - HUD.Size.Y.Offset)
local PrimaryPartXY = Camera:WorldToViewportPoint(Model.PrimaryPart.Position)

Move.Position = UDim2.new(0, PrimaryPartXY.X - BillboardTopLeft.X, 0, PrimaryPartXY.Y - BillboardTopLeft.Y)

No errors, but the button still is a ways away from the mouse
com-video-to-gif


Hopefully this image shows why. For some reason the button is being put in the top corner??

Even tho it’s position is like so here

And if I dont edit it’s position at all


It’s in the correct spot, just not where the mouse is

Ok i went into studio and set up a scenario similar to yours. I got it working with this code

local BillboardCenter = Camera:WorldToViewportPoint(HUD.Adornee.PrimaryPart.Position)
local BillboardTopLeft = Vector2.new(BillboardCenter.X - HUD.Size.X.Offset/2, BillboardCenter.Y - HUD.Size.Y.Offset/2)
local PrimaryPartXY = Camera:WorldToViewportPoint(mouse.Hit.p)
local Move = script.Parent.TextButton
	
Move.Position = UDim2.new(0, PrimaryPartXY.X - BillboardTopLeft.X, 0, PrimaryPartXY.Y - BillboardTopLeft.Y)