Help with Camera:WorldToScreenPoint()

For some reason, it’s not working as expected, i’m trying to get results as a BillboardGui
https://gyazo.com/f58066e4b194e1669f7158bc09796f87
But this happens
This is code:

self.GuiConnections['loop'] = Runservice.RenderStepped:Connect(function()
			if self.ShowGuiOBJECT then
				if (not self.Gui.Show.Visible) then
					self.Gui.Show.Visible = true
				end
			local WST = workspace.CurrentCamera:WorldToScreenPoint(workspace.CookingSystem.Ingredients:FindFirstChild(self.StepInformation[self.Step]).Position)
				self.Gui.Show.Position = UDim2.new(0,WST.X,0,WST.Y)
			end
		end)

Do you know a better way to make this!?
Thanks.

1 Like

Can you be more specific on what your trying to achieve the topic is kind of vague.

I’m trying to make it follow exactly the part like a billboard gui but not what happening.

You need to account for the size of the UI object:

self.GuiConnections['loop'] = Runservice.RenderStepped:Connect(function()
	if self.ShowGuiOBJECT then
		if (not self.Gui.Show.Visible) then
			self.Gui.Show.Visible = true
		end
		local WST, isOnScreen = workspace.CurrentCamera:WorldToScreenPoint(workspace.CookingSystem.Ingredients:FindFirstChild(self.StepInformation[self.Step]).Position)
		if isOnScreen then
			local size = self.Gui.Show.AbsoluteSize
			self.Gui.Show.Position = UDim2.new(0, WST.X - size.x/2, 0, WST.Y - size.y/2)
		end
	end
end)

Similarly, an instance’s position (in this case, the part you’re referencing as a cooking object), is the centre of the object - you need to offset that position if you want it above / below.

Not only that, but you need to interpolate the size of the UI object based on the distance of the object to the screen - otherwise if you’re hundreds of studs away from the object, the UI object will still be just as large as it is. You can achieve this like:

local MAX_DISTANCE = 50
local NORMAL_SIZE  = self.Gui.Show.Size
self.GuiConnections['loop'] = Runservice.RenderStepped:Connect(function()
	if self.ShowGuiOBJECT then
		if (not self.Gui.Show.Visible) then
			self.Gui.Show.Visible = true
		end
		local WST, isOnScreen = workspace.CurrentCamera:WorldToScreenPoint(workspace.CookingSystem.Ingredients:FindFirstChild(self.StepInformation[self.Step]).Position)
		if isOnScreen and WST.z <= MAX_DISTANCE then
			local depth = WST.z
			self.Gui.Show.Size = NORMAL_SIZE * (depth / MAX_DISTANCE)
			local size = self.Gui.Show.AbsoluteSize
			self.Gui.Show.Position = UDim2.new(0, WST.X - size.x/2, 0, WST.Y - size.y/2)
		end
	end
end)
1 Like

Hey! It turned out on a error
09:46:30.183 - ReplicatedStorage.CookingSystem.OOP - Cooking Handler:60: attempt to perform arithmetic (mul) on UDim2 and number

This part:

self.Gui.Show.Size = NORMAL_SIZE * (depth / MAX_DISTANCE)

Ah. Just change it so that:

local MAX_DISTANCE = 50
local NORMAL_SIZE  = self.Gui.Show.AbsoluteSize
self.GuiConnections['loop'] = Runservice.RenderStepped:Connect(function()
	if self.ShowGuiOBJECT then
		if (not self.Gui.Show.Visible) then
			self.Gui.Show.Visible = true
		end
		local WST, isOnScreen = workspace.CurrentCamera:WorldToScreenPoint(workspace.CookingSystem.Ingredients:FindFirstChild(self.StepInformation[self.Step]).Position)
		if isOnScreen and WST.z <= MAX_DISTANCE then
			local depth = WST.z
			local cur   = NORMAL_SIZE * (depth / MAX_DISTANCE)
			self.Gui.Show.Size = UDim2.new(0, cur.x, 0, cur.y)
			local size = self.Gui.Show.AbsoluteSize
			self.Gui.Show.Position = UDim2.new(0, WST.X - size.x/2, 0, WST.Y - size.y/2)
		end
	end
end)
4 Likes