WorldToScreenPoint returns incorrect distance

This is printing 9 studs, even tho the prompt is over 300 studs away from my character. I press E from where my camera is in game, and it prints Can open merge 9.813133239746094 false

The red part is the EcoMerge.Prompt:GetPivot().Position. As you can see, it’s clearly a lot further than 9 studs away.


Reason I do this is I am trying to only have E work while a billboardgui is visible, so it needs to only work when that billboard is on screen.

UserInputService.InputBegan:Connect(function(input, GPE)
		if GPE then return end
		
		if input.KeyCode ~= Enum.KeyCode.E then return end
		
		local ScreenPos, a = Camera:WorldToScreenPoint(EcoMerge.Prompt:GetPivot().Position)
		local Distance = ScreenPos.Z
		if Distance > 25 or Distance < 0 then return end
		print("Can open merge", Distance, a)
		self:Open()
	end)
2 Likes

WorldToScreenPoint doesn’t ignore GUI inset. I think you meant to use WorldToViewportPoint.

1 Like

That shouldn’t create such a large disparity in distance though?

It would. 30 Pixels can differ a lot. Take a look:

image

This is at least a 50 stud difference if you raycast from the bottom and from the top of the indicator.

Keep in mind that the difference gets much, much longer than 50 studs in many scenarios.


Edit: I’ve misinterpreted your problem. I thought you were raycasting.

What’s the reason why you didn’t use magnitude?