Since the PlayerMouse is superseded, and usage of UserInputService is advised, it has been annoying to do stuff with the mouse. This module should help you with that. It wraps the mouse, replacing almost all of its features (stuff like PlayerMouse.Icon
cannot be replaced for example), makes some features better (TargetFilter
is a table!), and adds some additional functionalities. The module is here, the source code is here.
To get the mouse you have to use :GetMouse()
. The returned object is singleton so shouldn’t be a problem to call it again.
local mouse = require(script.Parent.MouseModule):GetMouse()
Properties
-
Vector2
Position
Position of mouse on screen -
Vector2
Delta
Change in screen position since last frame (similar toUIS:GetMouseDelta()
which only works if mouse is locked in center) -
CFrame
Hit
CFrame
of mouse in 3D space, oriented towards mouse -
Instance
Target
Object in 3D space the mouse is pointing at -
Enum.NormalId
TargetSurface
Indicates theNormalId
of theBasePart
surface at which the mouse is pointing -
Array
TargetFilter
Array (yay!) of objects ignored by the mouse when calculatingmouse.Hit
andmouse.Target
, also has some helper methods listed below -
Ray
UnitRay
ARay
directed towards the mouse’s world position, originating from theCamera
's world position -
CFrame
Origin
ACFrame
positioned at theWorkspace.CurrentCamera
and oriented toward the mouse`s 3D position. -
Vector2
ViewPortSize
Describes the width and the height of the game window in pixels -
Int
ViewPortSizeX
Describes the width of the game window in pixels -
Int
ViewPortSizeY
Describes the height of the game window in pixels -
String
Icon
The content ID of the image used as the mouse’s icon
Event
-
RBXScriptSignal
Button1Down
Fired when the left mouse button is pressed.mouse.Position
as parameter -
RBXScriptSignal
Button1Up
Fires when the left mouse button is released.mouse.Position
as parameter -
RBXScriptSignal
Button2Down
Fired when the right mouse button is pressed.mouse.Position
as parameter -
RBXScriptSignal
Button2Up
Fires when the right mouse button is released.mouse.Position
as parameter -
RBXScriptSignal
Button3Down
Fired when the mouse wheel is pressed.mouse.Position
as parameter -
RBXScriptSignal
Button3Up
Fires when the mouse wheel is released.mouse.Position
as parameter -
RBXScriptSignal
Move
Fired when the mouse is moved. -
RBXScriptSignal
HitChanged
Fires when themouse.Hit
property changes (very useful imo) -
RBXScriptSignal
WheelForward
Fires when the mouse wheel is scrolled backwards -
RBXScriptSignal
WheelBackward
Fires when the mouse wheel is scrolled forwards
Methods
-
RaycastResults
Raycast
(
RaycastParams
params,
Int
distance)
Casts a ray withparams
as raycast parameters starting from the 3D version of the 2D position of the mouse (mouse.Position
) towards the direction of the mousedistance
long.params
is by default{FilterDescendantsInstances = mouse.TargetFilter, FilterType = Enum.RaycastFilterType.Blacklist}
.distance
is by default1000
. -
AddToFilter
(
Instance|Array|String
a)
Ifa
is an instance, adds it tomouse.TargetFilter
. Ifa
is an array, adds all of its elementsmouse.TargetFilter
. Ifa
is a string, it will be considered a tag for theCollectionService
, and all objects tagged witha
will be inserted (Note: objects tagged witha
after calling this method are not automatically added and calling the method again is needed) -
RemoveFromFilter
(
Instance|Array|String
a)
Ifa
is an instance, removes it frommouse.TargetFilter
. Ifa
is an array, removes all of its elements frommouse.TargetFilter
. Ifa
is a string, it will be considered a tag for theCollectionService
, and all objects tagged witha
will be removed -
IsInFilter
(
Instance
a)
Checks ifa
is inmouse.TargetFilter
-
Vector2
GetDelta
()
Returns the change inmouse.Position
since last frame -
CFrame
GetVelocity
()
Returns the change inmouse.Hit
since last frame