Help fixing a Zoom Feature for a Character Inspecting GUI

Hello :wave:, 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!

1 Like

That is the problem, you are forcing the charater to look straight ahead, change this line to

ClonedChar:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, 0, ZoomDistance), Vector3.new(0,0,0) ) * CFrame.Angles(0, math.rad(ClonedChar.HumanoidRootPart.Orientation.Y + 180),0))
1 Like

Dude, thank you! I really appreciate your help!

math.rad(ClonedChar.HumanoidRootPart.Orientation.Y + 180)

So then this is what makes it face the same direction it was facing :+1:, now I know! I was so close yet so far off lol… Thank you once again @ollebolle1313!

2 Likes

No problem! By the way, you should mark my answer as the solution, that way people know that this question is solved and it’s easier for people with the same problem to find the answer.

1 Like

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