Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to obtain mouse delta?

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)

Wiki Article Referenced

1 answer

✓ Accepted answer

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.

1 comment

0

It was the starter control scripts

This discussion is preserved as it was. New answers, comments, and votes are closed.