How can I detect a change in Mouse.Hit.Position?

Hello
I need to act on changing of the Mouse.Hit.Position
I can listen to the event of user moving the mouse but this does not cover the case when the user moves without moving the mouse.
Is there some other way to detect the change of Mouse.Hit.Position?
I can use the Heartbeat event for example, but I am wondering whether there is a more performant solution?

Thanks

Something like this?

local lastpos = Vector3.new()

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

game:GetService("RunService").RenderStepped:Connect(function()
    local old = lastpos
    local new = mouse.Hit and mouse.Hit.Position
    if old ~= new then
        print("New mouse world position!")
    end
    lastpos = new
end)
1 Like

Unfortunately the verification will still be triggered on each RenderStepped/Heartbeat, but if there is no other way…
Another issue is that simple comparison of vectors here gives a lot of false positives. For some reason Roblox reports the position often with minor differences after the 5th decimal digit, which makes the vectors different in case of direct comparison, even when the player/mouse has not moved.
So some rounding of the separate vector components (x,y,z) seems needed.

1 Like