How do I make the "perfect" ViewportFrame?

Hello, im making a gui that shows the players inventory. I dont want to make tons of decals so im deciding to use Viewport Frames. Though I have ran into a issue where since not all the parts are the same size that every Viewport Frame is inside the part or to far away. How can I make it so no matter which part it is it gets the right position?

for v = 1, #items do
				local temp = script.Template:Clone()
				temp.Parent = holder
				temp.ItemName.Text = items[v].Name
				
				local newViewPortCamera = Instance.new("Camera", script)
				newViewPortCamera.CameraType = Enum.CameraType.Scriptable
				
				local model = items[v]:Clone()
				
				temp.ItemPicture.CurrentCamera = newViewPortCamera
				if model:IsA("Model") then
					newViewPortCamera.CFrame = model.PrimaryPart.CFrame --* CFrame.Angles(0, math.rad(180), 0)
					model.PrimaryPart.Orientation = Vector3.new(0,0,0)
				elseif model:IsA("Part") then
					newViewPortCamera.CFrame = model.CFrame --* CFrame.Angles(0, math.rad(180), 0)
					model.Orientation = Vector3.new(0,0,0)
				end
				newViewPortCamera.CFrame = newViewPortCamera.CFrame + Vector3.new(0,0,1)
				model.Parent = temp.ItemPicture
end

If the part is not rotated (or is at least a clean 90° increment) and you know the size, it can be done with some simple trigonometry. You probably can do it with other rotations, but that’s beyond my understanding.

What you’re essentially trying to solve is how far to push the model away from the camera (or push the camera away from the model).

I have a video about this here that goes a bit into solving the math:

7 Likes

Hi,

I’ve done something similar to that, I was showing different models for the inventory that were all sorts of different sizes and orientations. I ended up using a table that held the name of each model and the scaling and orientation to use, and that’s worked pretty well for me

Or as you have models there that you are cloning you could add a Vector3Value to each that has the orientation that’s right for it, and then use that. Might be a bit neater.

Hope that helps!