1. What I want to achieve?
Hello Roblox developers,
I’m currently working on a catalog avatar editor GUI for my game, but I’ve run into a camera-related issue. I’d like to fix this problem.
2. What is the issue?
I found and modified a script to position Camera.CameraSubject on the left side of the screen, and while it works to some extent, the BillboardGui elements are floating on the screen, and the camera is not properly fixed on the character.
Reference video: https://www.youtube.com/watch?v=Cw__iFmgf2o
Code:
# AvatarEditorController.luau
function AvatarEditorController:SetEditingAvatar(avatar: Model?)
StateStore:SetState({
editingAvatar = avatar,
})
local currentCamera = workspace.CurrentCamera
if not avatar then
Resize.Stop()
return
end
LocalPlayerUtils:SetMovement(false)
setCoreGuiEnableds(false)
Resize.Start()
currentCamera.CameraSubject = avatar
end
# Resize.luau
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local Resize = {}
Resize.Position = UDim2.fromScale(0.3, 0)
Resize.Size = UDim2.fromScale(1, 1)
local function UDim2Absolute(udim2)
local viewSize = Camera.ViewportSize
return Vector2.new((udim2.X.Scale * viewSize.X) + udim2.X.Offset, (udim2.Y.Scale * viewSize.Y) + udim2.Y.Offset)
end
function Resize._computePosition()
local viewSize = Camera.ViewportSize
local aspectRatio = viewSize.X / viewSize.Y
local offset = UDim2Absolute(Resize.Position)
local position = offset / viewSize
local hFactor = math.tan(math.rad(Camera.FieldOfView) / 2)
local wFactor = hFactor * aspectRatio
return position.X * wFactor * 2, position.Y * hFactor * 2
end
function Resize._computeSize()
local size = UDim2Absolute(Resize.Size) / Camera.ViewportSize
return size.X, size.Y
end
function Resize._getOffset()
local x, y = Resize._computePosition()
local w, h = Resize._computeSize()
return CFrame.new(0, 0, 0, w, 0, 0, 0, h, 0, x, y, 1)
end
local beforeFovMode = Camera.FieldOfViewMode
function Resize.Start()
Camera.FieldOfViewMode = Enum.FieldOfViewMode.MaxAxis
RunService:BindToRenderStep("ViewportCamera", Enum.RenderPriority.Camera.Value + 1, function()
Camera.CFrame = Camera.CFrame * Resize._getOffset()
end)
end
function Resize.Stop()
RunService:UnbindFromRenderStep("ViewportCamera")
Camera.FieldOfViewMode = beforeFovMode
end
return Resize