You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to convert a 3D world position to a 2D screen position, even if the 3D position is out of the screen -
What is the issue? Include screenshots / videos if possible!
2 Issues I found so far:
1 slight inaccuracies in the 2D position
2 When look away from the part, it appears at the center of the screen instead of the edges of the screen
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried look on the DevForum and found no one complaining about my issue, I have tried using ChatGPT only to get the same problem in a longer and different script, I have tried asking programmers I know all of them not helping at all.
No, The problem is not with the image label that I use to Display the position returned, Anchor point is 0.5,0.5, image is centered
local function closerNumber(x, a, b)
-- Calculate the absolute differences between x and a, and x and b
local distToA = math.abs(x - a)
local distToB = math.abs(x - b)
-- Check which distance is smaller and return the corresponding value
if distToA < distToB then
return a
else
return b
end
end
module.Get2DPosition = function(Position: Vector3)
local ScreenPosition, inView = workspace.CurrentCamera:WorldToScreenPoint(Position)
local ScreenSize = workspace.CurrentCamera.ViewportSize
if inView then
local Vector2Position = Vector2.new(math.clamp(ScreenPosition.X, 0, ScreenSize.X), math.clamp(ScreenPosition.Y, 0, ScreenSize.Y))
return UDim2.fromOffset(Vector2Position.X, Vector2Position.Y)
else
local Vector2Position = Vector2.new(math.clamp(ScreenPosition.X, 0, ScreenSize.X), math.clamp(ScreenPosition.Y, 0, ScreenSize.Y))
local scaleX = Vector2Position.X / ScreenSize.X
local scaleY = Vector2Position.Y / ScreenSize.Y
return UDim2.fromOffset(scaleX, scaleY)
-- Attempt 2:
--local x,y = math.clamp(ScreenPosition.X, 0, ScreenSize.X), math.clamp(ScreenPosition.Y, 0, ScreenSize.Y)
--local xDistance, yDistance = ScreenSize.X - x, ScreenSize.Y - y
--if xDistance > x then
-- x = ScreenSize.X
--end
--if yDistance > y then
-- y = ScreenSize.Y
--end
--local Vector2Position = Vector2.new(x,y)
-- Attempt 1:
----local Vector2Position = Vector2.new(closerNumber(x,0, ScreenSize.X), closerNumber(y, 0, ScreenSize.Y))
--return UDim2.fromOffset(Vector2Position.X, Vector2Position.Y)
end
end