Camera Zooming on Touch Devices

Hello!

I’m basically trying to recreate Roblox’s default camera behavior when it comes to zooming in and out on touch devices. Currently, I’m trying to achieve this by using UserInputService.TouchPinch and it’s scale parameter, but it feels unbalanced. I’m probably using it wrong, because I can zoom in way faster than I can zoom out. This is because the passed scale argument goes up to 10+ while zooming in, but only moves between 1 and 0 while zooming out.

This is the snippet of code that the wiki offers which has the same ‘unbalanced’ zooming:

local lastTouchScale = nil
local function TouchZoom(touchPositions, scale, velocity, state)
	if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
		local difference = scale - lastTouchScale
		cameraZoom = cameraZoom * (1 + difference)
		if cameraZoomBounds ~= nil then
			cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
		else
			cameraZoom = math.max(cameraZoom, 0)
        end
		UpdateCamera()
	end
	lastTouchScale = scale
end

Could anyone tell me how to use the scale parameter properly, that way the user can zoom in and out by equal amounts?

Thanks in advance.

I’ve solved the problem by manually calculating the magnitude between the two provided touchpositions. By doing this I can now zoom in and out by equal amounts.

local lastMagnitude = 0
userInputService.TouchPinch:Connect(function(touchPositions, scale, velocity, state)
	local currentMagnitude = (touchPositions[1] - touchPositions[2]).magnitude
	if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
		local difference = currentMagnitude - lastMagnitude
	end
	lastMagnitude = currentMagnitude
end)
7 Likes