I’m trying to use viewport frames to turn 3d items into 2d space and still keeping their orientation and position as if it was 3d. I’ve got most of it complete, but there’s one problem that I don’t know how to fix: the block isn’t centred in the viewport so it doesn’t line up with the 3d object.
The way to fix this is if I can have the 2d object always located in the middle of the viewport frame. The problem with this system is that it uses the cameras view so it won’t be in the centre of the screen unless you’re looking directly at it.
I’m not sure how to adjust it so that it’s in the centre of the viewport so I came here for assistance. This is what I’ve done so far:
Here is the code:
local itemModel = game.ReplicatedStorage.Assets.Loot_Model:FindFirstChild(item)
if itemModel then
local viewport = script.Parent.ViewportFrame
itemModel = itemModel:Clone()
itemModel.CFrame = cframe
itemModel.Parent = viewport
itemModel = itemModel:Clone()
itemModel.CFrame = cframe
itemModel.Parent = Workspace
local cam = Instance.new("Camera")
viewport.CurrentCamera = cam
cam.Parent = viewport
RunService.RenderStepped:Connect(function()
local worldPoint = cframe.Position
local vector, onScreen = CAMERA:WorldToScreenPoint(worldPoint)
local screenPoint = Vector2.new(vector.X, vector.Y)
local depth = vector.Z
cam.CFrame = CAMERA.CFrame
viewport.Position = UDim2.new(0, screenPoint.X, 0, screenPoint.Y)
end)
end