I'm trying to obtain the x Delta of the mouse when the mouse is locked to the center of the screen, however, when using the following code the output is always 0
.
According to the wiki, the delta value shouldn't be zero, even when the mouse is locked. I tried unlocking the mouse, and the delta still does not calculate.
Is there something I'm doing wrong here?
EDIT: Using inputObject.Position
yields the correct position, so it is solely the Delta not working correctly.
local uis = game:GetService("UserInputService") uis.MouseIconEnabled = false uis.MouseBehavior = Enum.MouseBehavior.LockCenter uis.InputChanged:connect(function(inputObject) if inputObject.UserInputType == Enum.UserInputType.MouseMovement then print(inputObject.Delta.x) end end)
Firstly, the default ROBLOX character control scripts modify the MouseBehavior
mode, so you must either disable those control scripts or perpetually set the mode you want, otherwise the mode will change at some point during execution.
Secondly, the Delta
field only has a value when the MouseBehavior
is LockCenter
or LockCurrentPosition
. If the mode has changed, delta won't be reported as you'd expect.
To calculate the mouse velocity when the mouse isn't locked, you can store a Vector2 of the previous position at each InputChanged
event and find the difference between it and the current position.