I am wanting to return a 2d position of where my model is on screen. This is so I can position and size UI over said model.
local Viewport = script.Parent.Viewport
-- Get WorldModel
local WorldModel = script:WaitForChild("WorldModel")
WorldModel:PivotTo(CFrame.new(0, 0, 0) * CFrame.Angles(math.rad(180), 0, math.rad(90)))
WorldModel.Parent = Viewport
-- Create Camera
local Camera = Instance.new("Camera")
Camera.CFrame = CFrame.new(
Vector3.new(0, 10, 0),
WorldModel:GetPivot().Position
)
Camera.Parent = Viewport
Viewport.CurrentCamera = Camera
--// Open Player card
local function Open()
InTween:Play()
InTween.Completed:Wait()
local vector, onScreen = Camera:WorldToScreenPoint(WorldModel:GetPivot().Position)
local screenPoint = Vector2.new(vector.X, vector.Y)
script.Parent.Frame.Position = UDim2.fromScale(screenPoint.X, screenPoint.Y)
print(screenPoint)
end
I get 0.500000298, -35.6624527
Whether this is scale or offset, idk, but either way, -35 is not right
The X and Y components of the returned Vector3 value represent scale from the top left hand corner of the screen and a Y axis scale of -35 means that the specified world point is currently out of view of the camera, you can use the second value returned by WorldToScreenPoint() to determine this. The second value is a Boolean value which represents whether or not the world point is currently visible from the camera’s perspective.
Even tho the TopLeftPart is positioned at the correct Vector3, the frame isn’t being positioned correctly.
If you look top left. There’s a small yellow part, that’s TopLeftPart. But as the frame increases, the TopLeftFrame (red square frame) slides off the edge of the screen