Wacky Viewport Frames

  1. What do you want to achieve?
    When in build mode and selecting different buildings to make, I would like there to be a “preview” of the image (in viewport frame) on the button.

  2. What is the issue?
    Rotation of the camera and position of both the object and the camera turn into a strange decimal far from what is intended.

P.S. the soil is pretty hard to see but trust it’s there in the first image lol

Desired goal: (I manually tweaked the settings after joining)
image

What happens:
image

local function setupBuildMode()
	local smoothPlacementButton = placementGui:WaitForChild("SmoothPlacement")
	local gridEnabledButton = placementGui:WaitForChild("GridEnabled")

	smoothPlacementButton.MouseButton1Click:Connect(function()
		if smoothPlacement then
			smoothPlacement = false
			smoothPlacementButton.Text = "Smooth placement: false"
		else
			smoothPlacement = true
			smoothPlacementButton.Text = "Smooth placement: true"
		end
	end)

	gridEnabledButton.MouseButton1Click:Connect(function()
		if gridEnabled then
			gridEnabled = false
			gridEnabledButton.Text = "Grid enabled: false"
		else
			gridEnabled = true
			gridEnabledButton.Text = "Grid enabled: true"
		end
	end)
	
	local count = 0
	local buildableButton = placementGui:WaitForChild("BuildableButton")
	local buildableButtons = {}

	for _, buildable in ipairs(buildables:GetChildren()) do
		local clonedGui = buildableButton:Clone()
		clonedGui.Parent = placementGui
		clonedGui.TextLabel.Text = buildable.Name:gsub("([a-z])([A-Z])", "%1 %2")
		clonedGui.Position = UDim2.new(
			clonedGui.Position.X.Scale,
			clonedGui.Position.X.Offset + (clonedGui.Size.X.Offset * 1.1) * count,
			clonedGui.Position.Y.Scale,
			clonedGui.Position.Y.Offset
		)
		
		task.wait(.1)
		
		local viewPort = clonedGui:WaitForChild("Viewport")
		local clonedBuildable = buildable:Clone()
		clonedBuildable.Parent = viewPort
		clonedBuildable.PrimaryPart.CFrame = CFrame.new(0,0,0)
		
		local camera = Instance.new("Camera", viewPort)
		camera.CFrame = CFrame.new(2, 3, 5)
		camera.CFrame = CFrame.Angles(-30, 25, 0)
		
		viewPort.CurrentCamera = camera
		
		count = count + 1
		buildableButtons[clonedGui] = buildable
	end
	
	return buildableButtons
end
1 Like

i believe it is just because of the some maths that i am too dumb to know about

i’m no CFrame master, but i think you have to use math.rad() to do angles.

the line should instead be
camera.CFrame = CFrame.Angles(math.rad(-30), math.rad(25), 0)

The problem is in these lines, the third line (where your setting the cframe using CFrame.Angles) overrides the second line.

Like @RainingMemory said, you can fix this by adding math.rad, and instead of setting the CFrame again, you need to multiply CFrame.Angles by CFrame.new, like this:

camera.CFrame = CFrame.new(2, 3, 5) * CFrame.Angles(math.rad(-30), math.rad(25), 0)
1 Like

image
image

Still a pretty odd decimal.

Heres the updated code:

local buildableButtons = {}
	
	for _, buildable in buildables:GetDescendants() do
		if buildable:IsA("Model") then
			local buttonClone = buildableButton:Clone()
			local textLabelClone = buttonClone:WaitForChild("TextLabel")
			local viewportClone = buttonClone:WaitForChild("Viewport")
			local typeScrollBar = backgroundGui:WaitForChild(buildable.Parent.Name)
			local siblingButtons = #typeScrollBar:GetChildren()
			buttonClone.Parent = typeScrollBar
			buttonClone.Visible = true
			textLabelClone.Text = buildable.Name
			
			local viewPortCamera = Instance.new("Camera", viewportClone)
			viewportClone.CurrentCamera = viewPortCamera
			viewPortCamera.CFrame = CFrame.new(2, 3, 5) * CFrame.Angles(math.rad(-30), math.rad(25), 0)
			
			local buildableClone = buildable:Clone()
			buildableClone.Parent = viewportClone
			
			buttonClone.Position = UDim2.new(
				0,
				buildableButton.Position.X.Offset + (siblingButtons * (buildableButton.Size.X.Offset + buttonSpacing)) + buttonSpacing,
				0,
				15
			)
			
			buildableButtons[buttonClone] = buildable
		end
	end
	return buildableButtons

maybe try setting its orientation/rotation without using CFrame?

 viewportCamera.Rotation = Vector3.new(-30, 25, 0)

see if that works because i really dont know :shock:

Sadly, no. character limittttt