How do viewport frames work?

i want to use viewport frames for my game, but im not entirely sure how they work scripting wise and/or UI wise. Im just looking for a simple explamation on how they work and how to use them. Im not really asking for any code but examples would be nice!

thank you for your time

5 Likes

I recommend looking through this article: ViewportFrame GUI

3 Likes

Just read through it. I kinda understand how it works. Is there any way to display the viewport rendering outside of a screen gui?

1 Like

Do you mean using a SurfaceGui or BillboardGui for example?

2 Likes

Yes! this is what i mean. Is there a way?

2 Likes

Yes! ViewportFrames support both SurfaceGui & BillboardGui.
However: Instead of partenting your Gui to your Instance in Workspace, you have to parent it to StarterGui and then set its “Adornee” property to the Instance you want to display the Gui on.

3 Likes

I see! So id have to parent the viewport to starter GUI? would i have to make more frames?

3 Likes

This is how your tree would look like:
Workspace > SamplePart
StarterGui > SurfaceGui > ViewportFrame

Set the “Adornee” property to the part:

Player.StarterGui.SurfaceGui.Adornee = game.Workspace.SamplePart
2 Likes

and i’d just set the viewports current camera as normal?

2 Likes

insert a camera inside of the frame and capture the module in it

its pretty simple if you ask me :upside_down_face:

2 Likes

i just tried this and got this error

2 Likes

I think you misspelled “Adornee” in your script, it says “Adormee” in your error message.

1 Like

even after editing the mistake it still errors, heres my code

local view = script.Parent
local UIObject = script.Parent.Parent
local mirrorPart = workspace.Mirror.Camera
local camera = Instance.new("Camera", view)

UIObject.Adornee = mirrorPart
view.CurrentCamera = camera

camera.CFrame = mirrorPart.CFrame

EDIT: I cant seem to read and i made a few mistakes, oops haha

2 Likes

Alright, to clarify a bit:
I’ve written a sloppy script to demonstrate how you could go about doing this.

Make sure not to use this in your game, this script just serves the purpose of being an example.

From a LocalScript in StarterPlayerScripts:

local player = game.Players.LocalPlayer

local screenPart = Instance.new("Part")
screenPart.Name = "Screen"
screenPart.Size = Vector3.new(24, 19, 18)
screenPart.Position = Vector3.new(63, 9.5, 33)
screenPart.Parent = workspace

local surfaceGui = Instance.new("SurfaceGui")
surfaceGui.Adornee = screenPart
surfaceGui.Parent = player:WaitForChild("PlayerGui")

local viewportFrame = Instance.new("ViewportFrame")
viewportFrame.Size = UDim2.new(1, 0, 1, 0)
viewportFrame.Parent = surfaceGui

local partToDisplay = Instance.new("Part")
partToDisplay.Color = Color3.new(0.25, 0.75, 1)
partToDisplay.Parent = viewportFrame

local viewportCamera = Instance.new("Camera")
viewportFrame.CurrentCamera = viewportCamera
viewportCamera.Parent = viewportFrame

viewportCamera.CFrame = CFrame.new(Vector3.new(0, 2, 12), partToDisplay.Position)

I hope this gives you an idea on how this works.

9 Likes

I see now! i understand. Thank you for this!

one more question tho! Whatever i want rendered on the frame has to be parented to the frame?

2 Likes

Correct. The only way to display objects in a ViewportFrame is to parent them to it.

3 Likes