Trouble Recreating Camera Pan

Hello!

Roblox Studio comes with this neat future where you can pan your camera around while holding your middle mouse button down. This is what I am talking about:

I am basically trying to recreate this, but using the right mouse button instead. Sadly, I have not had much success. Could anyone point me in the right direction?

Thanks in advance.

1 Like

Recent additions to the CFrame API have made this considerably easier - you can use UpVector and RightVector to get the camera’s relative “up” and “right” respectively, regardless of its angle.

You can UserInputService to find when the right mouse button is pressed and released, obviously, but it can also be used to lock the mouse in place and to find how much the mouse has moved between frames.

You’ll simply want to multiply the mouse delta by the camera’s up and right vectors every frame, and multiply the camera’s CFrame by that; use RenderStepped for this.

i would use the mouse.move event and not renderstepped for that

1 Like

That’s probably a better idea, actually, yeah. Been using and abusing it a lot recently.

This is the code I wrote based on your tips and it seems to work like a charm!

local userInputService = game:GetService("UserInputService")
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local camera = workspace.CurrentCamera
local rightMouseButtonDown = false

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton2 then
		userInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
		rightMouseButtonDown = true
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton2 then
		userInputService.MouseBehavior = Enum.MouseBehavior.Default
		rightMouseButtonDown = false
	end
end)

mouse.Move:Connect(function()
	if rightMouseButtonDown then
		local mouseDelta = userInputService:GetMouseDelta()
		camera.CFrame = camera.CFrame + 
			(camera.CFrame.RightVector * -mouseDelta.X)/10 +
			(camera.CFrame.UpVector * mouseDelta.Y)/10
	end
end)

Thank you, @Auhrii and @SquarePapyrus12!

2 Likes