How do i detect if a player is trying to drag the camera?

I have been working on a camerascript which allows you to move the camera to the left / right / up / down and scroll in and out, but i dont know how to detect if a player tries to drag the camera.


How do i detect if a player is trying to drag the camera?

2 Likes

Listening to MouseMovement with ContextActionService should work for this.

delta will be a blank vector when the mouse isn’t locked, but when it is it will represent how much the mouse was moved. UserInputService works for toggling the mouse behavior.

local ContextActionService = game:GetService"ContextActionService"
local UserInputService = game:GetService"UserInputService"
local blank = Vector3.new()
ContextActionService:BindAction("MouseMovement",function(name,state,input)
    local delta = input.Delta
    if delta == blank then return end
    print(delta)
    --do whatever with delta
end,false,Enum.UserInputType.MouseMovement)
ContextActionService:BindAction("Mouse2Hold",function(name,state,input)
    if state ==  Enum.UserInputState.Begin then
        UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
    elseif state == Enum.UserInputState.End or state == Enum.UserInputState.Cancel then
        UserInputService.MouseBehavior = Enum.MouseBehavior.Default
    end
end,false,Enum.UserInputType.MouseButton2)
4 Likes