マウス は、より広い範囲をカバーし、より機能性が高く、クロスプラットフォームパターンをより良くサポートする および に置き換えられました。広範囲に使用されているため、サポートされていますが、これらの代替を使用することを強く検討する必要があります。
マウス オブジェクトは、主にボタンとレイキャスト用の様々な APIを宿しています。それは Player:GetMouse() を介してアクセスでき、Players.LocalPlayer で LocalScript に呼び出されます。また、Tool.Equipped イベントによっても伝達されます。
- カーソルの外外見を変更する Icon プロパティが最も注目に値します。
- It continually raycasts the screen mouse position into the 3D world using the TargetFilter プロパティ, storing the results of the raycast in the Hit , Target , and TargetSurface プロパティ.これらは単純な場合に有用ですが、WorldRoot:Raycast() より複雑な レイキャスト シナリオでは使用する必要があります。
-- ローカルスクリプトから:local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- マウスアイコンを設定するmouse.Icon = "rbxasset://SystemCursors/Wait"
注意:
このオブジェクトは、ポインタの移動を制御/制限しません。このためには、UserInputService.MouseBehavior および UserInputService.MouseDeltaSensitivity を参照してください。
2つの機能が同じ入力イベントに接続されている場合、例えば 、 両方の機能はイベントが発動すると実行されます。イベントはこの動作をサポートしていないため、沈没/パスインプットの概念はありません。しかし、ContextActionService は、BindAction を通じてこの動作を持っています。
マウスはすべてのプラットフォームで利用できない場合がありますが、マウスは通常、マウスまたはポインターハードウェアがないモバイル (タッチ) とコンソール (ゲームパッド) でも機能します。明示的なクロスプラットフォーム動作には、UserInputService と ContextActionService を使用します。
エクスペリエンスで入力をカスタマイズする方法については、入力とカメラ を参照してください。
概要
プロパティ
3D 空間でのマウスの位置の CFrame。
Mouse アイコンとして使用される画像のコンテンツID。
A CFrame は、Workspace.CurrentCamera に配置され、マウスの 3D 位置に向かって向けられます。
3D 空間のオブジェクト mouse が指しているもの。
Mouse.Hit と Mouse.Target を決定するときに、オブジェクト (およびその子孫) を無視することを決定します。
マウスがポイントしている Enum.NormalId 表面の BasePart を示します。
A Ray マウスの世界位置に向かって、Workspace.CurrentCamera 世界位置から発生する。
ゲームウィンドウのピクセル幅を説明します。
ゲームウィンドウの高さをピクセルで記述します。
画面上のマウスの位置の X (横) コンポーネントを説明します。
マウスの画面位置の Y (垂直) コンポーネントを説明します。
イベント
左マウスボタンが押されたときに発火します。
左マウスボタンがリリースされると発火します。
右マウスボタンが押されると発火します。
右マウスボタンがリリースされると発射されます。
マウスが別のマウスイベントにパスされていない期間のすべてのハートビートで発射されます。
マウスが移動すると発射されます。
マウスホイールが後ろにスクロールするときに発火します。
マウスホイールが前方にスクロールされると発火します。
プロパティ
Hit
このプロパティは、CFrame マウスの位置を 3D 空間に示します。注意してください、Mouse.TargetFilter およびその子孫は無視されます。
開発者は、次のようにヒットの位置を取得できます:
local position = mouse.Hit.Position
ヒットは Tools によってしばしば、3人称でマウスに向けて武器を発射するために使用されます。
マウスが指している BasePart を検索している開発者は、Mouse.Target を使用する必要があります。
Mouse.Hit はどのように計算されますか?
ヒット Cフレームの位置は、マウスの内部 Ray (拡張バージョンの Mouse.UnitRay ) と 3D 空間のオブジェクト (パーツなど) の交点として計算されます。
ヒット Cフレームの向きは、Mouse.UnitRay の方向と一致します。
local unitRayDirection = mouse.UnitRay.Directionlocal mouseHitDirection = mouse.Hit.lookVector-- unitRayDirection ≈ mouseHitDirection-- the vectors are approximately equal
注: のロールは、ヒットの方向を計算するときには使用されません。
マウスの内部線は 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 を偽に設定します。
エクスペリエンスでユーザーのマウスアイコンを取得/設定するには、UserInputService.MouseIcon を使用する必要があります。Mouse.Icon は、新しいプラグイン API でマウスカーソルを設定することができるようになった後、廃止されます。
カーソルを設計する
次のガイドラインは、自分のマウスカーソルを作成するときに役立つ可能性があります:
- 使用された画像の寸法は、カーソルのサイズを決定します。
- 画像の 中央 は、マウスの入力が発行される場所です。
- デフォルトのマウス画像は 64x64 ピクセルで、マウスが 17x24 ピクセルのスペースを占有します。
システムカーソル
から回収された を使用すると、手、矢印、Iビームなど、システムのデフォルトカーソルと同じようなアイコンを使用できますGUIイベント(MouseEnter、MouseLeave、MouseButton1Downなど)を使用して、特定の種類のGUIコンポーネントと対話するときに一貫した 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 は、Origin と同じ位置から開始し、同じ方向のスタッドで拡張します。
local unitRay = mouse.UnitRaylocal origin = mouse.Origin-- unitRay.Direction = 起点.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
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 (a 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 (a 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
このイベントは、プレイヤーが左マウスボタンを押すと発動します。これは からアクセスできます;例えば、 に配置されたとき、左マウスボタンが押されるたびに以下のコードが印刷されます。
local tool = script.Parent -- ツールオブジェクトであることを確認する
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
print("Button1Down")
end)
end)
世界空間でマウスの位置を見つけ、BasePart および Hit および Target プロパティを使用して指を向いているかどうかを確認できます。
Button1Up
このイベントは、プレイヤーが左マウスボタンをリリースすると発動します。これは からアクセスできます;例えば、 に配置されたとき、左マウスボタンがリリースされるたびに以下のコードが印刷されます。
local tool = script.Parent -- ツールオブジェクトであることを確認する
tool.Equipped:Connect(function(mouse)
mouse.Button1Up:Connect(function()
print("Button1Up")
end)
end)
世界空間でマウスの位置を見つけ、BasePart および Hit および Target プロパティを使用して指を向いているかどうかを確認できます。
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 イベントの間で発射します。
For information on how to obtain the Mouse オブジェクト, please see the 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
マウスが移動すると発射されます。
注: このイベントは、マウスの位置が更新されると発動しますので、移動中に複数回発動します。
For information on how to obtain the Mouse オブジェクト, please see the 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
ホイールバックワードイベントは、マウスホイールが後ろにスクロールすると発動します。このイベントの可能な使用法には、1人称シューター (FPS) で銃のスコープを切り替えたり、プレイヤーのカメラをズームしたりすることが含まれます。
これは、スクロール前方イベントと一緒に使用できます、Mouse.WheelForward。
For information on how to obtain the Mouse オブジェクト, please see the 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
ホイールフォワードイベントは、マウスホイールが前方にスクロールされると発動します。このイベントの可能な使用法には、1人称シューター (FPS) で銃のスコープを切り替えたり、プレイヤーのカメラをズームしたりすることが含まれます。
これは、スクロールバックイベントと一緒に使用できます、Mouse.WheelBackward。
For information on how to obtain the Mouse オブジェクト, please see the 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)