Camera/Model Issue With Viewports

Hello, I’m currently having a problem with viewports and positioning either the model or the camera. I’m trying to make a menu with a ton of swords displayed inside viewports, but I’m not sure how. Here’s what I’ve accomplished so far:

Images

weapons
weapons2

You can see that the swords do appear in the viewports, but they’re all oriented differently. In the first image, you can see that the swords are facing downwards towards the camera. In the second image, containing different sword models, they’re facing upwards (the swords are positioned from the same script).

What I’m currently trying to figure out is how to get them all to face the camera in the correct orientation. Here’s the code I used to create what I have now:

Code
local sword = weaponsDict[tostring(currentSword)]
local newSwordSlot = swordSlot:Clone()
local viewportFrame = newSwordSlot.SwordViewport

local viewportCamera = Instance.new("Camera", viewportFrame)
viewportCamera.CameraType = Enum.CameraType.Scriptable
viewportFrame.CurrentCamera = viewportCamera

local viewportSword = getModel(sword, "Handle") -- Returns a model version of the sword where PrimaryPart is the handle.
-- Basically just takes all the baseparts in an object and returns them within a model.
local viewportPoint = Vector3.new(0, 0, 0)

viewportSword:SetPrimaryPartCFrame(CFrame.new(viewportPoint))
viewportSword.Parent = viewportFrame

local rotCon
rotCon = runService.Heartbeat:Connect(function()
	local cframe, size = viewportSword:GetBoundingBox()

	local max = math.max(size.X, size.Y, size.Z)
	local dist = (max/math.atan(math.rad(viewportCamera.FieldOfView))) * .5
	local currentDist = (max/2) + dist

	local currentRot = 0

	if currentRot >= 360 then
		currentRot = 0
	end
	currentRot += 1

	viewportCamera.CFrame = CFrame.Angles(0, math.rad(currentRot), 0) * CFrame.new(viewportPoint + Vector3.new(0, currentDist*.6, currentDist), viewportPoint)
end)
table.insert(rotateConnections, rotCon)
newSwordSlot.Parent = swordFrame

When I paste the swords into the workspace, they all appear to be oriented the same way, so I’m not sure how this happened. Please let me know if you have any suggestions!

2 Likes

im not sure how to fix but there is a plugin that you can see its rotation,distant and size of object of viewport in real-time

There’s a great post and code for this by Egomoose found here.