Hello, developers. I just released some new API which should make grabbing mouse input easier for most purposes. Included in this change are 4 new methods to UserInputService that allow you to keep track of the buttons that are pressed, the location of the mouse, and the delta:
array<InputObject> UserInputService:GetMouseButtonsPressed()
-- returns an array of active pressed down mousebuttons
bool UserInputService:IsMouseButtonDown(UserInputType mouseButton)
-- returns a bool of whether or not the provided mouse button is pressed down.
Vector2 UserInputService:GetMouseLocation()
-- returns the screen position of the mouse; topbar not taken into account
Vector2 UserInputService:GetMouseDelta()
-- returns the change in the mouse's position since the previous frame
-- Note: like the Delta property of an InputObject, this only works when the moue is locked.
Thanks for reading, and be sure to give them a try. If you notice any problems with any of the API, or have any questions pertaining to them, please report them to me.
Ooh, wonderful! Glad that UserInputService is now able to handle the mouse’s position. Riddance’s the use of Player:GetMouse() in a lot more cases. Thanks for this!
yeah, its just raycasting, but compare the 3-5 lines for the raycasting to the 2 words for mouse.target and you’ll see why it’s useful to have something like this
Mouse.Target is kind of archaic because it uses mouse.TargetFilter to filter mouse rays, which can only be a single instance. It’s much more powerful if your TargetFilter can be a list, which is easy to implement with camera:ScreenPointToRay().
function getMouseTargetWithFilter(cursorPosition, filter)
local ray = camera:ScreenPointToRay(cursorPosition.x, cursorPosition.y, 900)
return workspace:FindPartOnRayWithIgnoreList(ray, filter)
end
If your code is well organized and modular, the difference between 3-5 lines and two words won’t matter because this should only appear in any of your code in one place in your entire game.
Yeah, most of it is my own laziness!
However most devs won’t switch to this whilst mouse.target is still supported and no better method available (for the same reason)
When can we expect to see :GetMouseWheelDelta()? It would be really advantageous to be able to determine how far the player has scrolled the mouse wheel and in what direction.
Not sure what you mean, but the top left of the top bar, or the true origin, is located at 0,0. The beginning of the in-game area is 0,36. (if you use GetMouseLocation)
I feel like this has an obvious answer, but what’s wrong with the current player mouse? It seems to work fine for me and isn’t deprecated or anything either? Are you just trying to make one standardized service for all the different inputs?
Well, we already had similar methods for grabbing keyboard and gamepad input, so we decided to include mouse input in that as well. So I suppose it’s some sort of standardization. Also afaik none of these methods would do anything to deprecate the Mouse object or pull people away from using it if they already do. They just add simpler ways to grab mouse input without having to set up hooks for the UserInputService Began, Changed and Ended events.
I recently allowed inputs to pass through the top bar when it is disabled, so I think I will probably be able to do this soon. I really want to do it before my internship ends.