Mouse
The Mouse class is part of the Input package.
local Mouse = require(packages.Input).Mouse
Properties
LeftDown
EventMouse.LeftDown:
Signal
LeftUp
EventMouse.LeftUp:
Signal
RightDown
EventMouse.RightDown:
Signal
RightUp
EventMouse.RightUp:
Signal
MiddleDown
EventMouse.MiddleDown:
Signal
MiddleUp
EventMouse.MiddleUp:
Signal
Moved
Eventmouse.Moved:Connect(function(position) ... end)
Scrolled
Eventmouse.Scrolled:Connect(function(scrollAmount) ... end)
Functions
new
Constructs a new mouse input capturer.
local mouse = Mouse.new()
IsLeftDown
Mouse:
IsLeftDown
(
) →
boolean
Checks if the left mouse button is down.
IsRightDown
Mouse:
IsRightDown
(
) →
boolean
Checks if the right mouse button is down.
IsMiddleDown
Mouse:
IsMiddleDown
(
) →
boolean
Checks if the middle mouse button is down.
GetPosition
Gets the screen position of the mouse.
GetDelta
Gets the delta screen position of the mouse. In other words, the distance the mouse has traveled away from its locked position in a given frame (see note about mouse locking below).
Only When Mouse Locked
Getting the mouse delta is only intended for when the mouse is locked. If the
mouse is not locked, this will return a zero Vector2. The mouse can be locked
using the mouse:Lock()
and mouse:LockCenter()
method.
GetRay
Returns the viewport point ray for the mouse at the current mouse position (or the override position if provided).
Raycast
Mouse:
Raycast
(
) →
RaycastResult?
Performs a raycast operation out from the mouse position (or the
overridePos
if provided) into world space. The ray will go
distance
studs forward (or 1000 studs if not provided).
Returns the RaycastResult
if something was hit, else returns nil
.
Use Raycast
if it is important to capture any objects that could be
hit along the projected ray. If objects can be ignored and only the
final position of the ray is needed, use Project
instead.
local params = RaycastParams.new()
local result = mouse:Raycast(params)
if result then
print(result.Instance)
else
print("Mouse raycast did not hit anything")
end
Project
Gets the 3D world position of the mouse when projected forward. This would be the
end-position of a raycast if nothing was hit. Similar to Raycast
, optional
distance
and overridePos
arguments are allowed.
Use Project
if you want to get the 3D world position of the mouse at a given
distance but don't care about any objects that could be in the way. It is much
faster to project a position into 3D space than to do a full raycast operation.
local params = RaycastParams.new()
local distance = 200
local result = mouse:Raycast(params, distance)
if result then
-- Do something with result
else
-- Raycast failed, but still get the world position of the mouse:
local worldPosition = mouse:Project(distance)
end
Lock
Mouse:
Lock
(
) →
(
)
Locks the mouse in its current position on screen. Call mouse:Unlock()
to unlock the mouse.
Must explicitly unlock
Be sure to explicitly call mouse:Unlock()
before cleaning up the mouse.
The Destroy
method does not unlock the mouse since there is no way
to guarantee who "owns" the mouse lock.
LockCenter
Mouse:
LockCenter
(
) →
(
)
Locks the mouse in the center of the screen. Call mouse:Unlock()
to unlock the mouse.
Must explicitly unlock
See cautionary in Lock
method above.
Unlock
Mouse:
Unlock
(
) →
(
)
Unlocks the mouse.
Destroy
Mouse:
Destroy
(
) →
(
)
Destroys the mouse.