Help with Mouse.Delta

I have a few questions about the Mouse.Delta:

  1. Why use it?

  2. When to use?

  3. what is it?

3 Likes

Try using the developer page;

Info:
Delta is a property of InputObject and function of UserInputService

3 Likes

Mouse.Delta, and anything that has the word “delta” in it (in math denoted as Δ something, basically saying “delta somthing”), means “change in that something”. By how much a quantity changed since the last iteration. For example you’re mouse was at a certain point in the screen A which was at (2, 4) (a Vector2), then the next frame it went to another point B which was at (8, 4), mouse.delta would be how much the position of the mous changed,
B-A = (8-6, 4-4) = (6, 0)
then mouse.delta would be (6, 0).
I assume the mouse.delta is re-calculated every frame.

And delta can describe change of anything really, we can have delta time, Δtime, meaning change in time, how much time passed since we last noted down the time. RenderStepped gives deltatime as a parameter, it’s how much time passed since the last frame was rendered.

You can use the mouse.delta for a lot of things really. For example, you can use it to make a sensor that goes up when the mouse moves a lot, and goes down when it doesn’t move. You can do this by checking if the mouse.delta.magnitude (like in the example above with points A and B, the result of mouse.delta is a vector, mouse.delta.magnitude the length of that vector which represents the distance the mouse moved since the last frame) and change the height of the sensor’s ui relative to the length of the mouse.delta.

local frame = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local previousPosition = Vector2.new(mouse.X, mouse.Y) --there is no mouse.Position, so we need to do this
game:GetService("RunService").RenderStepped:Connect(function() --a renderstepped
    local delta = (Vector2.new(mouse.X, mouse.Y) - previousPosition).Magnitude --our delta, new position minus old one
    frame:TweenSize(UDim2.fromOffset(frame.Size.X.Offset, -math.min(240, delta)), 0.03) --I do math.min  so the sensor doesn't go beyond a certain limit which is 240
    previousPosition = Vector2.new(mouse.X, mouse.Y) --next time previous position is the current one
end)

4FTO2SnHat

Not the best but can be polished.

(Also there isn’t even a property called .Delta of the mouse object, I just used it so it’s easier to follow, to get the mouse’s delta use this, this method only works if UserInputService.MouseBehavior is set to Enum.MouseBehavior.LockCenter, in the script above I get the delta by myself by simply subtracting the previous mouse position from the new one, vice versa also works)

19 Likes

Thanks that helped me though it wasn’t my post.

3 Likes