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?
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’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.
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)