Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to obtain mouse delta?

Asked by
Sublimus 992 Moderation Voter
7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by
duckwit 1404 Moderation Voter
7 years ago

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.

0
It was the starter control scripts Sublimus 992 — 7y
Ad

Answer this question