How to detect if camera is rotating left or right?

I made a script that can already do what the title states. It is as shown here:

As you can see, the camera tilts towards the direction you turn.
The problem with this is that it takes in the MouseInput, which only works for PC users. On mobile and console, this feature does not work. I am wondering if theres an alternative to what I’m doing here:

deltaX = 0
deltaLerp = 0
context:BindAction("CameraMovement", function(_,_,input)
	deltaX = strafeResult + (input.Delta.X/200)*lookIntensity
end, false, Enum.UserInputType.MouseMovement)

function cameraLookTilt()
	deltaLerp = deltaLerp + (deltaX-deltaLerp)*.1
	cam.CFrame = cam.CFrame * CFrame.Angles(0, 0, deltaLerp) 
	print(deltaX)
end

runservice.RenderStepped:Connect(function(deltaTime)
	cameraLookTilt()
end)

It doesn’t look like there’s a way to do it through the ContextActionService, or at least not an obvious one. Maybe Enum.UserInputType.Touch, but I don’t think so.

UserInputService.TouchMoved might work for you.

You could also detect the angle the camera changed in the Y axis in the last frame, if you want:

local cam = workspace.CurrentCamera

local function Flatten(vec: Vector3): Vector2
	return Vector2.new(vec.X, vec.Z).Unit
end

local lastDir = Flatten(cam.CFrame.LookVector)

local function ReadCameraDelta()
	local dir = Flatten(cam.CFrame.LookVector)
	-- positive for clockwise rotations, negative for ccw
	local angle = math.asin(lastDir:Cross(dir))
	print(string.format("angle change = %d", math.deg(angle)))
	lastDir = dir
end

game:GetService("RunService"):BindToRenderStep("UpdateCamera", Enum.RenderPriority.Camera.Value + 1, ReadCameraDelta)

But a input-based method would probably be better for your application.

8 Likes

Thank you, the suggestion you made with TouchMoved worked. I now have concerns about console but I’m happy with this.

You could also root around in Roblox’s camera scripts and find how they’re detecting input, might give some insight.

This can also be done for both axis!

(Sorry if the orginization looks like a war crime)


	local function Flatten(vec: Vector3,vector1:string,vector2:string): Vector2
                #turning vector 3 into vector two
		return Vector2.new(vec[vector1], vec[vector2]).Unit
	end
	local function Lerp(a, b, m)
		return a + (b - a) * m
	end

	local lastDirZ = Flatten(camera.CFrame.LookVector,"X","Z")
	local lastDirY = Flatten(camera.CFrame.LookVector,"Z","Y")



	local angleX
	local angleY
	local function ReadCameraDelta()
		local dir = Flatten(camera.CFrame.LookVector,"X","Z")
		local dir2 = Flatten(camera.CFrame.LookVector,"Z","Y")

		-- positive for clockwise rotations, negative for ccw
		angleX = math.asin(lastDirZ:Cross(dir))
		angleY = math.asin(lastDirY:Cross(dir2))
		angleX = math.deg(angleX)
		angleY = math.deg(angleY)

	    print(string.format("angle change X = %d", math.deg(angleX )))
	    print(string.format("angle change Y = %d", math.deg(angleY )))

		lastDirZ = dir
		lastDirY = dir2
	end