滑鼠 已被 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。
一個 CFrame 位置在 Workspace.CurrentCamera 並面向滑鼠標的 3D 位置。
指向 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 使用來向第三人稱鼠標發射武器。
尋找鼠標指向的開發者應該使用 。
如何計算 Mouse.Hit?
命中 CFrame 的位置計算為滑鼠標內部的 Ray (擴展版本的 Mouse.UnitRay ) 和 3D 空間中的對象 (例如零件) 之間的交點。
命中CFRAM的方向與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.MouseIconEnabled為 false。
要在體驗中獲得/設置使用者鼠標圖示,您應該使用 UserInputService.MouseIcon 。Mouse.Icon將在發布新API用於插件設置滑鼠指針後被淘汰。
設計指標器
以下指引在創建自己的滑鼠指標時可能有用:
- 使用的圖像尺寸決定了鼠標的大小。
- 圖像的 中心 是發出滑鼠輸入的地方。
- 預設滑鼠圖像為 64x64 像素,滑鼠佔用 17x24 像素空間。
系統指標器
當使用從 Plugin:GetMouse() 中擷取的 PluginMouse 時,您可以使用與系統預設箭頭、手、I 梁等類似的圖示,如下所示:您可以使用這些與 GUI 事件,例如 MouseEnter , MouseLeave 和 MouseButton1Down 來提供與特定類型的 GUI 組件互動時一致的 Studio 體驗。請注意,這些只適用於 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 。
範例程式碼
The following code sample, when placed in StarterPlayerScripts will create a tool in the player's backpack that, once equipped, will change the BasePart.BrickColor of every BasePart the player clicks on.
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
單位射線屬性是一個 Ray 指向 3D 空間中滑鼠標位置的 (由 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 對象。
範例程式碼
This example demonstrates how mouse events are passed during each frame
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 對象。
範例程式碼
The example below allows the local player to move parts with their mouse.
When the player presses their left mouse button over a part, that part is the mouse's target and becomes the point. Until the player releases their left mouse button, that part will move to the mouse's world position when the player moves their mouse.
Note that the Mouse.TargetFilter property allows the code to ignore the part being moved when determining the mouse's world position.
The code should work as expected when placed in a 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 onWheelBackward()
print("Wheel went forward!")
end
mouse.WheelForward:Connect(onWheelBackward)