I have a few questions about the Mouse.Delta:
-
Why use it?
-
When to use?
-
what is it?
I have a few questions about the Mouse.Delta:
Why use it?
When to use?
what is it?
Try using the developer page;
Info:
Delta is a property of InputObject and function of UserInputService
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)
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)
Thanks that helped me though it wasn’t my post.