Hi, so for a camera that is draggable/swipe-able along only one axis (no rotation) I’ve put this together.
With how I’ve done it, the camera movement stops immediately after the mouse button is lifted, I don’t want this. In an attempt to add some ‘inertia’ to keep moving the camera after a strong swipe I’ve added a tween when the mouse is lifted. The results are decent, but not perfect.
Currently, the camera sort of just follows the mouse, and holding it still at the side lets the camera drift. How would I make the camera movement more like its being dragged by the mouse being held down?
camera localscript
local R = game:GetService("RunService")
local cam = workspace.CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()
local UIS = game:GetService("UserInputService")
local MouseOriginal
local Mouse2
local Mouse3
local Buttondown
local Movement
local TS = game:GetService("TweenService")
local Style = Enum.EasingStyle.Cubic
function pan()
Mouse2 = mouse.X
local Distance1 = MouseOriginal-Mouse2
Movement = Distance1/500 --random number, dunno how to make the mouse distance into a reasonable X value.
cam.CFrame = cam.CFrame + Vector3.new(Movement,0,0)
end
mouse.Button1Down:Connect(function()
Buttondown = true
MouseOriginal = mouse.X
end)
mouse.Button1Up:Connect(function()
Buttondown = false
Mouse3 = mouse.X
local Distance2 = Mouse2 - Mouse3
local TI = TweenInfo.new(1,Style)
local Goal = {CFrame = cam.CFrame + Vector3.new(Distance2*2,0,0)} -- another random manipulation of the mouse distance that gave a reasonable movement.
local Tween = TS:Create(cam,TI,Goal)
Tween:Play() -- Tween attempts to create swipe "inertia" after letting go, remove to just have the camera stop immediately after mouse up
end)
R.RenderStepped:Connect(function()
cam.CameraType = "Scriptable"
if Buttondown then
pan()
end
end)
Please advice if there is a better way to do this, I’m completely new to UIS and screensize stuff. Cheers!