鼠标 已被 UserInputService 和 ContextActionService 取代,这些覆盖了更广的范围,更富有功能,且支持 跨平台 模式更好。由于其广泛使用,它仍然受到支持,但你应该强烈考虑使用这些替代方案。
鼠标 对象包含各种 API 用于指针,主要用于按钮和射线投射。它可以通过 Player:GetMouse() 调用在 Players.LocalPlayer 中的 LocalScript 访问。它还通过 Tool.Equipped 事件传递。
- 最显著的是 Icon 属性,它会改变鼠标的外观。
- 它不断将屏幕鼠标位置射出到 3D 世界,使用 TargetFilter 属性存储射线投射的结果,存储在 Hit、Target 和 TargetSurface 属性中。这些可能对简单的情况有用,但 WorldRoot:Raycast() 应在更复杂的 射线投射 场景中使用。
-- 从本地脚本:local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- 设置鼠标图标mouse.Icon = "rbxasset://SystemCursors/Wait"
注意:
该对象不控制/限制指针移动。有关此内容,请参阅 UserInputService.MouseBehavior 和 UserInputService.MouseDeltaSensitivity 。
如果两个函数连接到同一个输入事件,例如 , 两个函数都会在事件发生时运行。由于事件不支持沉没/传递输入的概念,因此无法执行此操作。然而,ContextActionService 通过 BindAction 拥有这种行为。
虽然鼠标在所有平台上可能不可用,但鼠标仍然会在移动设备(触摸)和控制台(游戏手柄)上工作,这通常没有鼠标或指针硬件。对于明确的跨平台行为,请使用 UserInputService 和 ContextActionService 。
请参阅输入和相机获取有关体验中自定义输入的更多信息。
概要
属性
鼠标位置在 3D 空间的 CFrame。
用作Mouse图标的图像内容ID。
位于 并面向鼠标 3D 位置的 A。
在 3D 空间中指向的对象 mouse 。
决定在确定 Mouse.Hit 和 Mouse.Target 时忽略对象 (和其子孙)。
指示鼠标指向的 Enum.NormalId 表面上的 BasePart 。
一个 Ray 指向鼠标世界位置,起源于 Workspace.CurrentCamera 世界位置。
描述游戏窗口的宽度以像素为单位。
描述游戏窗口的高度以像素计。
描述鼠标位置在屏幕上的 X(横向)组件。
描述鼠标屏幕位置的 Y(垂直)组件。
活动
当左鼠标按钮被按下时发火。
当左鼠标按钮释放时发火。
当按下右键时发生火焰。
当右键按钮释放时发射。
在鼠标不被传递到另一个鼠标事件期间,每次心跳时发射。
当鼠标移动时发射。
当鼠标轮向后滚动时发生火焰。
当鼠标轮滚动向前时发射。
属性
Hit
该属性表示鼠标在 3D 空间的位置 CFrame 。请注意,Mouse.TargetFilter 和其子孙将被忽略。
开发者可以通过以下方式获取命中的位置:
local position = mouse.Hit.Position
命中 часто由 Tools 用于向第三人称鼠标发射武器。
寻找鼠标指向的开发者应使用 。
鼠标命中如何计算?
命中 CFrame 的位置计算为鼠标内部的 Ray (扩展版本的 Mouse.UnitRay ) 和 3D 空间中的对象 (例如零件) 之间的交点。
命中的 CFrame 的方向与 Mouse.UnitRay 的方向相对应。
local unitRayDirection = mouse.UnitRay.Directionlocal mouseHitDirection = mouse.Hit.lookVector-- unitRayDirection ≈ mouseHitDirection-- the vectors are approximately equal
注意,在计算命中 Workspace.CurrentCamera 的方向时,不使用滚动 CFrame 。
鼠标的内部射线延伸到 1,000 个单位。如果鼠标不指向 3D 空间中的对象(例如指向天空时),该属性将与 Workspace.CurrentCamera 相距 1,000 个单位。
代码示例
The code in this sample, when placed inside a LocalScript within StarterPlayerScripts will draw a red laser beam between the character's head and Mouse.Hit at all times.
Note, this beam will pass directly through obstructions in third person as the Mouse's raycasting is done from the Workspace.CurrentCamera not the head.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
beam.FaceCamera = true
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
beam.Parent = workspace.Terrain
attachment0.Parent = workspace.Terrain
attachment1.Parent = workspace.Terrain
local function onRenderStep()
local character = player.Character
if not character then
beam.Enabled = false
return
end
local head = character:FindFirstChild("Head")
if not head then
beam.Enabled = false
return
end
beam.Enabled = true
local origin = head.Position
local finish = mouse.Hit.Position
attachment0.Position = origin
attachment1.Position = finish
end
RunService.RenderStepped:Connect(onRenderStep)
The code below visualizes the difference between Mouse.Hit and Mouse.Origin. In order to do this, the code uses the Vector3 positions of the hit and origin CFrame values using .p.
The difference is that the origin is "where the mouse came from" (its origin) and the hit is the position where the mouse hits (is when the player presses their mouse).
This example also visualizes that the mouse origin is very similar to the position of the CurrentCamera by printing the magnitude (distance) between the two positions.
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local mouse = player:GetMouse()
local camPos = camera.CFrame.Position
local function onButton1Down()
print("Mouse.Hit:", mouse.Hit.Position)
print("camPos:", camPos)
print("Mouse.Origin:", mouse.Origin.Position)
print("Magnitude:", (mouse.Origin.Position - camPos).Magnitude)
end
mouse.Button1Down:Connect(onButton1Down)
Icon
图标 是决定用作指针的图像的属性。如果为空,将使用默认箭头。当滚动器悬停在 GuiButton 上时,此属性暂时被忽略。
要完全隐藏鼠标, 不要 使用透明图像,而是将UserInputService.MouseIconEnabledfalse。
要获取/设置体验中的用户鼠标图标,你应该使用 UserInputService.MouseIcon 。Mouse.Icon将在新API为插件设置鼠标滚轮后被淘汰。
设计鼠标
以下指南在创建自己的鼠标指针时可能有用:
- 使用的图像尺寸决定了鼠标的大小。
- 图像的尺寸不应超过任何轴的 256 像素。
- 图像的 中心 是鼠标输入发出的地方。
- 默认鼠标图像为 64x64 像素,鼠标占用 17x24 像素空间。
系统指针
当使用从 Plugin:GetMouse() 中提取的 PluginMouse 时,您可以使用与系统默认鼠标类似的图标,例如手、箭、I 梁等你可以使用这些与图形用户界面事件,例如 MouseEnter , MouseLeave , 和 MouseButton1Down 来提供与特定类型的图形用户界面组件互动时一致的 Studio 体验。请注意,这些仅适用于工作室插件;它们不适用于其他 Mouse 对象。
<th>资产</th><th>建议使用</th></tr></thead><tbody><tr><td><img src="../../../assets/misc/Mouse-Icon-Pointer.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Arrow</code></td><td>默认单击和选择。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-PointingHand.png" width="30"></img></td><td><code>rbxasset://SystemCursors/PointingHand</code></td><td>将鼠标悬停在活动链接/按钮上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-OpenHand.png" width="30"></img></td><td><code>rbxasset://SystemCursors/OpenHand</code></td><td>将鼠标悬停在可拖动的项目上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-GrabbingHand.png" width="30"></img></td><td><code>rbxasset://SystemCursors/ClosedHand</code></td><td>拖动一个物品。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-IBeam.png" width="30"></img></td><td><code>rbxasset://SystemCursors/IBeam</code></td><td>在文本字段中悬停。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeNS.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeNS</code></td><td>将鼠标悬停在垂直缩放手柄上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeEW.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeEW</code></td><td>将鼠标悬停在横向调整手柄上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeNESW.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeNESW</code></td><td>将鼠标悬停在角落上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeNWSE.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeNWSE</code></td><td>将鼠标悬停在角落上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeAll.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeAll</code></td><td>将鼠标悬停在多向调整手柄上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeSplitV.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SplitNS</code></td><td>将鼠标悬停在垂直“分割”手柄上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeSplitH.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SplitEW</code></td><td>将鼠标悬停在横向“分割”手柄上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Forbidden.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Forbidden</code></td><td>将鼠标悬停在一个锁定/禁用的项目上。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Wait.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Wait</code></td><td>表示正在进行的行动。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Busy.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Busy</code></td><td>表示系统正在忙碌。</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Crosshair.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Cross</code></td><td>将鼠标悬停在精确选择区域上。</td></tr></tbody>
查看* |
---|
代码示例
This example changes the Players.LocalPlayer mouse icon to look like a dragon image.
local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
mouse.Icon = "http://www.roblox.com/asset?id=163023520"
Origin
起源 Mouse 属性是指示鼠标起源的 CFrame 。它位于 Workspace.CurrentCamera 并面向鼠标的 3D 位置。
Mouse.UnitRay 从同一位置开始与起源,并延伸到同一方向的一格。
local unitRay = mouse.UnitRaylocal origin = mouse.Origin-- 单位射线.方向 = 起源.p-- unitRay.Direction ≈ origin.lookVector
对于 3D 空间中 Mouse 的位置,请参阅 Mouse.Hit 。
代码示例
The code below visualizes the difference between Mouse.Hit and Mouse.Origin. In order to do this, the code uses the Vector3 positions of the hit and origin CFrame values using .p.
The difference is that the origin is "where the mouse came from" (its origin) and the hit is the position where the mouse hits (is when the player presses their mouse).
This example also visualizes that the mouse origin is very similar to the position of the CurrentCamera by printing the magnitude (distance) between the two positions.
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local mouse = player:GetMouse()
local camPos = camera.CFrame.Position
local function onButton1Down()
print("Mouse.Hit:", mouse.Hit.Position)
print("camPos:", camPos)
print("Mouse.Origin:", mouse.Origin.Position)
print("Magnitude:", (mouse.Origin.Position - camPos).Magnitude)
end
mouse.Button1Down:Connect(onButton1Down)
Target
在 3D 空间中指向的对象 mouse 。
注意:
- 如果 Mouse.TargetFilter 已设置,目标过滤器和其子集将被忽略。
- 当鼠标不指向 BasePart 时,例如当它指向天空时,目标将是 nil 。
- 寻找 3D 空间中鼠标位置的开发人员应使用 Mouse.Hit。
代码示例
以下代码示例,放置在 StarterPlayerScripts 中将创建一个工具,一旦装备,将改变玩家单击的每个 BasePart.BrickColor 的 BasePart。
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local backpack = localPlayer:WaitForChild("Backpack")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.CanBeDropped = false
tool.Parent = backpack
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target.Parent then
mouse.Target.BrickColor = BrickColor.random()
end
end)
end)
TargetFilter
该属性决定对象在计算 Mouse.Hit 和 Mouse.Target 时被鼠标忽略。对象的后裔也被忽略,因此可以忽略多个对象,只要它们是对象的后裔,该属性被设置到的对象。这个属性对于包含特殊效果或装饰的模型有用,不应影响 Mouse.Hit 或 Mouse.Target 。
该属性可以设置为任何 Instance 或 nil , 例如:
local Players = game:GetService("Players")local Workspace = game:GetService("Workspace")local player = Players.LocalPlayerlocal mouse = player:GetMouse()mouse.TargetFilter = Workspace.Model
请注意, Character 的 Players.LocalPlayer 被鼠标自动忽略。
TargetSurface
该属性表示鼠标指向的 Enum.NormalId 表面上的 BasePart 特性。这个属性由鼠标的世界位置( Mouse.Hit )和鼠标指向的部分( Mouse.Target )得到。
当鼠标不指向零件时,此属性无意义,例如当鼠标指向天空时。目前,在这些情况下,此属性设置为“右”。使用此属性之前,请检查鼠标的目标不是 nil 。
local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- 检查是否存在指向鼠标的部分if mouse.Target thenprint("The mouse is pointing to the " .. mouse.TargetSurface.Name .. " side of " .. mouse.Target.Name)elseprint("The mouse is not pointing at anything.")end
代码示例
The code in this sample, when placed in a LocalScript inside StarterPlayerScripts will set the surface of any BasePart clicked on to a random surface.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local surfaceTypes = {
Enum.SurfaceType.Smooth,
Enum.SurfaceType.Glue,
Enum.SurfaceType.Weld,
Enum.SurfaceType.Studs,
Enum.SurfaceType.Inlet,
Enum.SurfaceType.Universal,
Enum.SurfaceType.Hinge,
Enum.SurfaceType.Motor,
}
local function onMouseClick()
-- make sure the mouse is pointing at a part
local target = mouse.Target
if not target then
return
end
local surfaceType = surfaceTypes[math.random(1, #surfaceTypes)]
local surface = mouse.TargetSurface
local propertyName = surface.Name .. "Surface"
mouse.Target[propertyName] = surfaceType
end
mouse.Button1Down:Connect(onMouseClick)
UnitRay
单位射线属性是指向 3D 空间鼠标位置的 Ray (由 Mouse.Hit 描述)。它起源于 CFrame 的 Workspace.CurrentCamera 。与所有单位射线一样,它有 1 的距离。
local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()print(mouse.UnitRay.Direction.Magnitude) -- Always 1
ViewSizeX
ViewSizeX 属性描述游戏窗口的横向组件尺寸以像素为单位。
代码示例
This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.
local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
ViewSizeY
ViewSizeY 属性描述游戏窗口的垂直尺寸以像素计。该长度包括顶部栏所使用的空间。
代码示例
This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.
local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
当检测到鼠标在屏幕上的位置发生变化时,建议您使用 ContextActionService:BindAction() 或 Enum.UserInputType.MouseMovement 或 UserInputService.InputChanged,其中均描述了鼠标的位置使用 Position (一个 Vector3 ) 的 InputObject,而不是使用这些和相关属性。
X 属性描述了鼠标在屏幕上的横向组件位置。位置以像素计算,与左上角相对,在顶部栏下。这个属性可以与 Mouse.Y 结合,生成一个 Vector2 代表鼠标位置:
local position = Vector2.new(mouse.X, mouse.Y)
此属性不会触发 Changed 或从 GetPropertyChangedSignal 返回的信号。使用 Mouse.Move 事件代替。
代码示例
This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.
local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
当检测到鼠标在屏幕上的位置发生变化时,建议您使用 ContextActionService:BindAction() 或 Enum.UserInputType.MouseMovement 或 UserInputService.InputChanged,其中均描述了鼠标的位置使用 Position (一个 Vector3 ) 的 InputObject,而不是使用这些和相关属性。
Y 属性描述了鼠标在屏幕上的垂直组件位置。位置以像素计算,与左上角相对,在顶部栏下。这个属性可以与 Mouse.X 结合,生成一个 Vector2 代表鼠标位置:
local position = Vector2.new(mouse.X, mouse.Y)
此属性不会触发 Changed 或从 GetPropertyChangedSignal 返回的信号。使用 Mouse.Move 事件代替。
代码示例
This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.
local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
方法
活动
Button1Down
当玩家按下左键时,此事件发生。请注意,这可以从 Tool 访问;例如,当放置在 LocalScript 中时,下面的代码每当左键按下时都会打印 Button1Down 。
local tool = script.Parent -- 确保这是一个工具对象
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
print("Button1Down")
end)
end)
您可以使用 BasePart 和 Hit 属性查找鼠标在世界空间的位置,以及它是否指向任何 Target 。
Button1Up
当玩家释放左鼠标按钮时,此事件发生。请注意,这可以从 Tool 访问;例如,当放置在 LocalScript 中时,下面的代码每当左键按钮释放时都会打印 Button1Up 。
local tool = script.Parent -- 确保这是一个工具对象
tool.Equipped:Connect(function(mouse)
mouse.Button1Up:Connect(function()
print("Button1Up")
end)
end)
您可以使用 BasePart 和 Hit 属性查找鼠标在世界空间的位置,以及它是否指向任何 Target 。
Button2Down
当玩家按下右键时,此事件发生。请注意,这可以从 Tool 访问;例如,当放置在 LocalScript 中时,下面的代码每当按下右键时都会打印 Button2Down 。
local tool = script.Parent -- 确保这是一个工具对象
tool.Equipped:Connect(function(mouse)
mouse.Button2Down:Connect(function()
print("Button2Down")
end)
end)
您可以使用 BasePart 和 Hit 属性查找鼠标在世界空间的位置,以及它是否指向任何 Target 。
Button2Up
当玩家释放右键时,此事件发生。请注意,这可以从 Tool 访问;例如,当放置在 LocalScript 中时,下面的代码会在释放右键时打印 Button2Up 。
local tool = script.Parent -- 确保这是一个工具对象
tool.Equipped:Connect(function(mouse)
mouse.Button2Up:Connect(function()
print("Button2Up")
end)
end)
您可以使用 BasePart 和 Hit 属性查找鼠标在世界空间的位置,以及它是否指向任何 Target 。
Idle
在鼠标不被传递到另一个鼠标事件期间,每次心跳时发射。
注意,此事件不应用于确定鼠标何时静止。因为它每次发射每次心跳,它将在 Mouse.Move 事件之间发射。
有关如何获取 Mouse 对象的信息,请参阅 Mouse 页面。
开发者可以在世界空间中找到鼠标的位置,如果它指向任何 BasePart 使用 Mouse.Hit 和 Mouse.Target 属性。
注意,开发人员建议在新工作中使用 UserInputService 而不是 Mouse 对象。
代码示例
这个例子显示了鼠标事件在每个框架中如何传递
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local events = {
"Button1Down",
"Button1Up",
"Button2Down",
"Button2Up",
"Idle",
"Move",
"WheelBackward",
"WheelForward",
"KeyDown",
"KeyUp",
}
local currentEvent
local frame = 0
local function processInput()
frame = frame + 1
print("Frame", frame, "- mouse event was passed to", currentEvent)
end
for _, event in pairs(events) do
mouse[event]:Connect(function()
currentEvent = event
end)
end
RunService:BindToRenderStep("ProcessInput", Enum.RenderPriority.Input.Value, processInput)
Move
当鼠标移动时发射。
注意,此事件会在鼠标位置更新时触发,因此在移动时会反复触发。
有关如何获取 Mouse 对象的信息,请参阅 Mouse 页面。
开发者可以在世界空间中找到鼠标的位置,如果它指向任何 BasePart 使用 Mouse.Hit 和 Mouse.Target 属性。
mouse.Move:Connect(function()
local position = mouse.Hit.p
local target = mouse.Target
print(target, position)
end)
注意,开发人员建议在新工作中使用 UserInputService 而不是 Mouse 对象。
代码示例
下面的例子允许 local player 使用鼠标移动零件。
当玩家按下左键在零件上时,该零件是 mouse's target 并成为 点 。直到玩家释放左键按钮,该部分在玩家移动鼠标时将移至鼠标的世界位置。
请注意,Mouse.TargetFilter 属性可以让代码在确定鼠标世界位置时忽略移动的部分。
代码应在 LocalScript 中放置时按期望的方式运行。
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local point
local down
local function selectPart()
if mouse.Target and not mouse.Target.Locked then
point = mouse.Target
mouse.TargetFilter = point
down = true
end
end
local function movePart()
if down and point then
local posX, posY, posZ = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
point.Position = Vector3.new(posX, posY, posZ)
end
end
local function deselectPart()
down = false
point = nil
mouse.TargetFilter = nil
end
mouse.Button1Down:Connect(selectPart)
mouse.Button1Up:Connect(deselectPart)
mouse.Move:Connect(movePart)
WheelBackward
当鼠标轮向后滚动时,后轮事件发生,当鼠标轮向后滚动时,后轮事件发生这个事件的可能用途包括在第一人称射击游戏(FPS)中切换枪的瞄准镜或缩放玩家的相机。
这可以与向前滚动的事件一起使用,Mouse.WheelForward。
有关如何获取 Mouse 对象的信息,请参阅 Mouse 页面。
注意,开发人员建议在新工作中使用 UserInputService 而不是 Mouse 对象。
代码示例
The below example assumes that you have already got the player's mouse (and set it as a variable named 'mouse'), whether by use of a Tool, HopperBin or the Player:GetMouse() method. It will print "Wheel went backwards!" when the player scrolls backwards.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onWheelBackward()
print("Wheel went backwards!")
end
mouse.WheelBackward:Connect(onWheelBackward)
WheelForward
当滚轮向前滚动时,滚轮向前事件触发,当滚轮向前滚动时。这个事件的可能用途包括在第一人称射击游戏(FPS)中切换枪的瞄准镜或缩放玩家的相机。
这可以与向后滚动的事件一起使用,Mouse.WheelBackward。
有关如何获取 Mouse 对象的信息,请参阅 Mouse 页面。
注意,开发人员建议在新工作中使用 UserInputService 而不是 Mouse 对象。
代码示例
The below example assumes that you have already got the player's mouse (and set it as a variable named 'mouse'), whether by use of a Tool, HopperBin or the Player:GetMouse() method. It will print "Wheel went forward!" when the player scrolls forwards.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onWheelForward()
print("Wheel went forward!")
end
mouse.WheelForward:Connect(onWheelForward)