Hello , I have a bug in my code and I just don’t know how to fix it because it’s my first time working with ViewportFrames.
Every time I make the character face a different direction and then zoom in or out, it makes the character face forward. I’ve tried using the camera for zooming in but it just messes everything up and I don’t know if I’m doing it the wrong way or the right way just missing some stuff. This is my first time making something like so I would really appreciate some help!
This is the code I got so far (I got some stuff from YouTube trying to understand how Viewports work)
I also tried adding notes to most of the stuff I sort of knew what I was doing with
local UserInputService = game:GetService("UserInputService")
local ViewPortFrame = script.Parent.MainFrame.ClothingView.ViewportFrame
-- Function to load a character into the viewport
function LoadCharacterIntoViewport(character)
local MouseInDisplay, HoldInDisplay = false, false
local Current_X_Axis
local ZoomSpeed = 0.25 -- Adjust the zoom speed
local MinZoomDistance = -10 -- Minimum zoom distance
local MaxZoomDistance = -2.5 -- Maximum zoom distance
local ZoomDistance = -7.5 -- Initial zoom distance
local IsResettingView = false -- Track if the view is being reset
local IsZoomEnabled = true -- Track whether zooming is enabled
local InHeadView = false
-- Camera
local VPFcam = Instance.new("Camera")
VPFcam.Parent = ViewPortFrame
VPFcam.CFrame = CFrame.new(0, 0, 0)
ViewPortFrame.CurrentCamera = VPFcam
repeat wait(.1) until game:IsLoaded()
character.Archivable = true
local ClonedChar = character:Clone()
ClonedChar.Parent = ViewPortFrame.WorldModel
ClonedChar.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
ClonedChar:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, 0, ZoomDistance), Vector3.new(0, 0, 0)))
-- Turning Feature (X Axis only)
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
if MouseInDisplay == true then
HoldInDisplay = true
Current_X_Axis = nil
end
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
HoldInDisplay = false
end
end)
ViewPortFrame.MouseMoved:Connect(function(X, Y)
if HoldInDisplay == false then return end
if Current_X_Axis then
ClonedChar.PrimaryPart.CFrame *= CFrame.fromEulerAnglesXYZ(0, ((X - Current_X_Axis) * 0.025), 0)
end
Current_X_Axis = X
end)
ViewPortFrame.MouseEnter:Connect(function() MouseInDisplay = true end)
ViewPortFrame.MouseLeave:Connect(function() MouseInDisplay = false end)
-- Camera Zooming Feature
local function onMouseWheelZoom(input)
if not MouseInDisplay then
return -- Don't zoom when mouse in not in side of the viewport
end
local zoomDelta = input.Position.Z
-- Adjust the zoom distance within the limits
ZoomDistance = math.clamp(ZoomDistance + (zoomDelta * ZoomSpeed), MinZoomDistance, MaxZoomDistance)
ClonedChar:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, 0, ZoomDistance), Vector3.new(0, 0, 0)))
end
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseWheel then
onMouseWheelZoom(input)
end
end)
-- Button for resetting the view
script.Parent.MainFrame.ClothingView.ActionButtons.ResetView_Buttom.MouseButton1Click:Connect(function()
VPFcam.CFrame = CFrame.new(0, 0, 0)
ZoomDistance = -7.5
ClonedChar:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, 0, ZoomDistance), Vector3.new(0, 0, 0)))
InHeadView = false
end)
-- Button for focusing camera on the character's head
script.Parent.MainFrame.ClothingView.ActionButtons.HeadView_Buttom.MouseButton1Click:Connect(function()
local head = ClonedChar:FindFirstChild("Head")
if head and InHeadView == false then
VPFcam.CFrame = CFrame.new(0, 0, 0)
local headCFrame = head.CFrame
local newCFrame = CFrame.new(headCFrame.p - Vector3.new(0, 0, -5), headCFrame.p)
VPFcam.CFrame = newCFrame
InHeadView = true
elseif InHeadView then
VPFcam.CFrame = CFrame.new(0, 0, 0)
InHeadView = false
end
end)
end
local characterToLoad = game.Workspace:FindFirstChild("Rig1")
LoadCharacterIntoViewport(characterToLoad)
Here is a video of my exact issue:
Thank you to anyone who takes a shot at helping me!