How to adjust a parts CFrame?

hello, I’m trying to automatically adjust the CFrame of a part inside a viewport frame so I don’t have to do it manually.

I have some code but the part is too far away from the viewport frame. here is what it looks like:
image

Here is the code:

local part = v:Clone()
local offset = Vector3.new(2, 2, 0)
local cf, size = v:GetBoundingBox()
local sizeX, sizeY, sizeZ = size.X, size.Y, size.Z
local halfSize = math.max(sizeX, sizeY) * 0.5
local dist = halfSize / 100 + (sizeZ * 0.5)
part:PivotTo(CFrame.new((cf * CFrame.new(Vector3.zAxis * dist + offset)).Position , cf.Position))
part.Parent = clone.ViewportFrame

Have you tried setting the camera CFrame to the part instead of moving the part to the camera?

ViewportFrame.CFrame = part.CFrame - Vector3.new(0,0,1)

you may need to change the vector3 to face the part so do some changes until you can see the part.

CFrame is not a valid member of ViewportFrame “Players.SAKDEVRBLX_alt.PlayerGui.ScreenGui.main.Builds.ScrollingFrame.222.ViewportFrame”

1 Like

mb, forgot camera


ViewportFrame.Camera.CFrame = part.CFrame - Vector3.new(0,0,1)

20:59:14.865 Camera is not a valid member of ViewportFrame “Players.SAKDEVRBLX_alt.PlayerGui.ScreenGui.main.Builds.ScrollingFrame.2.ViewportFrame” - Client - LocalScript:130

do I have to add a camera in there manually>?

Make sure you create a Camera for a Viewmodel

Example:

local Cam = Instance.new("Camera"); Cam.Parent = ViewportFrame
Cam.CFrame = CFrame.new(0,0,1)

ViewportFrame.CurrentCamera = Cam


Okay I did this but the models are still way too far away from the viewport frame. How would I fix this?

Make sure you Set the Parts distance closer to the Camera by Subtracting the Said Parts Position by the Z axis

Or, Alternatively,
Make them Bigger

no matter how much I increase the Z axis, it doesn’t change. here’s my code:

local clone = scrolling.Template:Clone()
local Cam = Instance.new("Camera")
local part = v:Clone()
part.Parent = clone.ViewportFrame
clone.Parent = scrolling
Cam.Parent = clone
Cam.CFrame = part.PrimaryPart.CFrame - Vector3.new(0,0,1000)

Try this:

part:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,-5), Vector3.new(0,0,0)))

This is a piece of code from me using a ViewportFrame to Position my Character
Change it to whatever you like

it works but is it possible to automatically determine the correct position using math? this is because it’s better for some parts and worse for others.

for example, My flooring is completely out of the picture


Yet my walls are at their ideal position

I’m not sure about that, I know its possible but I’m not sure on how to do that for every single item

Try this, I got this from somewhere in devforum:

Cam.Parent = clone

local cf, size = part:GetBoundingBox()
local Max = math.max(Size.X,Size.Y,Size.Z)
local Distance = (Max/math.tan(math.rad(Cam.FieldOfView))) * 2.6 -- Change this number to change the distance if you're having problems
local CurrentDistance = (Max/2) + Distance

Cam.CFrame = CFrame.new(part:GetPrimaryPartCFrame().p - Vector3.new(0,0,CurrentDistance),part:GetPrimaryPartCFrame().p)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.