How would I make the camera zoom towards the character correctly?

So I’m making a 2.5D game, and I want you to be able to zoom the camera by using the mousewheel. It isn’t working as I intended, and just makes the camera behave weirdly, and doesn’t zoom in towards the character. Here’s my code:

local camRef = game.Workspace:WaitForChild("TestStage"):WaitForChild("CamRef")
local humanoidDiv = 10 -- What to divide the humanoidRootPart.Position by, to make the camera move a little towards the player and make it more dynamic

local zoomZ = 0 -- The amount of zoom

runService.RenderStepped:Connect(function()
	local tween = tS:Create(
		cam, 
		camTI, 
		{CFrame = CFrame.new(camRef.Position+Vector3.new(0,0,camRef.Size.X/3-zoomZ)+humanoidRootPart.Position/(humanoidDiv+zoomZ)+Vector3.new(1,1,1)*zoomZ, camRef.Position+humanoidRootPart.Position/(humanoidDiv-zoomZ)+Vector3.new(1,1,0)*zoomZ)}
	):Play()
	--cam.CFrame = CFrame.new(camRef.Position+Vector3.new(0,2,camRef.Size.X/2), humanoidRootPart.Position)
	
	local mHit = mouse.Hit
	if mHit.Position.X < humanoidRootPart.Position.X then
		humanoidRootPart.CFrame = CFrame.Angles(0, -80.1, 0) * ( humanoidRootPart.CFrame - humanoidRootPart.CFrame.Position ) + humanoidRootPart.CFrame.Position
	elseif mHit.Position.X > humanoidRootPart.Position.X then
		humanoidRootPart.CFrame = CFrame.Angles(0, 80.1, 0) * ( humanoidRootPart.CFrame - humanoidRootPart.CFrame.Position ) + humanoidRootPart.CFrame.Position
	end
end)

mouse.WheelForward:Connect(function()
	zoomZ -= 1
end)

mouse.WheelBackward:Connect(function()
	zoomZ += 1
end)

Any way I can fix this? I can give more info if requested.

1 Like