Using TouchPan to pan camera relative to camera rotation

Hello. I’ve been trying to make a camera system for mobile devices and it works to some extent, but it does not take into account rotation of the camera.

I can’t screen record on my mobile device, so I will just explain the issue. If you, for example, swipe your finger up on a mobile device, it moves the camera to the left, and vice versa. I’ve been trying to make it so that it accounts for the rotation of the camera, so no matter what rotation the camera is in, it will always make it so that swiping up pans the camera downwards (and swiping left moves your camera rightwards, etc).

I couldn’t exactly find anything on this topic anywhere, especially for using UIS mobile functions. The code below is adapted from the wiki example. I haven’t been able to figure out how to get the panning to be relative to the camera orientation. When the camera faces a certain way, it works completely fine and just the way I want it to, but when the camera is rotated facing a different direction, it pans in the wrong directions.

wait(3)

local UIS = game:GetService('UserInputService')
local cam = workspace.CurrentCamera
local part = workspace.Part

cam.CFrame = CFrame.lookAt(part.Position + Vector3.new(0, 100, 0), part.Position)
cam.CameraType = Enum.CameraType.Scriptable

local lastTranslation = 0

local touchPan = function(touchPositions, totalTranslation, velocity, state, gameProcessed)
	if (state == Enum.UserInputState.Change or state == Enum.UserInputState.End) and not gameProcessed then
		local difference = totalTranslation - lastTranslation
		
		cam.CFrame += Vector3.new(difference.X, 0, difference.Y)
	end
	
	lastTranslation = totalTranslation
end

UIS.TouchPan:Connect(touchPan)

1 Like