マウス は UserInputService と ContextActionService によって代わりました。これらは、より広い範囲をカバーし、より機能性の高い、クロスプラットフォーム対応のパターンをサポートしています。それらの代替を強く推奨するためにサポートされますが
マウスオブジェクトは、ポインターのためのさまざまな API をホストします。主にボタンとレイキャストのためです。 Class.Player:GetMouse() は、Class.Players.LocalPlayer の Class.LocalScript で呼び出されます。 2>Class.Tool.Equipped2> イ
- Class.Mouse.Icon|Icon プロパティは、カーソルの外外見を変更する
- Class.Mouse.TargetFilter|TargetFilter プロパティを使用して、3D 世界のスクリーンマウスポジションを持続的にレイキャストし、結果を Hit
-- ローカルスクリプトから:local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- マウスアイコンを設定するmouse.Icon = "rbxasset://SystemCursors/Wait"
注:
このオブジェクトはポインターの動きを制御/制限しません。For これには、UserInputService.MouseBehavior とUserInputService.MouseDeltaSensitivity を参照してください。
Class.Mouse.Button1Down|Button1Down などのイベントイベントに接続された 2つの関数が同じイベントを発生させると、両方の関数が実行されます。 Class. Context Action Service は、イベントがこの動作をサポートしないため、Class. Context Action Service
モバイルプラットフォームではマウスが利用できない場合がありますが、マウスはモバイル (タッチ) とコンソール (ゲームパッド) の両方で機能します (通常はマウスやポインターハードウェアがない)。詳細なクロスプラットフォーム動作は、UserInputService とContextActionService を参照してくだ
エクスペリエンスで入力のカスタマイズに関する詳細は、入力とカメラ を参照してください。
概要
プロパティ
マウスの位置の 3D 空間の中の CFrame の位置。
画像の Class.Mouse アイコンのコンテンツ ID。
Class.Workspace.CurrentCamera の位置にあり、マウスの 3D 位置に向けてオリエントされた A Workspace.CurrentCamera。
3D 空間のオブジェクトは、mouse です。
オブジェクト (そしてその子孫) をイグノールするオブジェクト (とその子孫) をイグノールするオブジェクトを決定します。
マウスがポイントしている Enum.NormalId の表面の BasePart を示します。
A Ray マウスの世界の位置に向かって、Workspace.CurrentCamera の世界の位置から起源します。
ゲームウィンドウの幅をピクセル単位で説明します。
ゲームウィンドウの高さをピクセル単位で説明します。
マウスの位置の X コンポーネントを説明します。
マウスのスクリーンポジションの Y コンポーネントを説明します。
イベント
左マウスボタンが押されたときに発動します。
左マウスボタンがリリースされるときに発動します。
右マウスボタンが押されるときに発動します。
右マウスボタンがリリースされると発射されます。
マウスが別のマウスイベントに渡されていない間に、心拍数があるたびに発生します。
マウスが移動すると発射されます。
マウスホイールが後ろにスクロールされるときに発動します。
マウスホイールが前方にスクロールされるときに発動します。
プロパティ
Hit
このプロパティは、3D 空間のマウスの位置について CFrame を指します。注意してください Mouse.TargetFilter そしてその子孫は無視されます。
開発者は、次のようにヒットの位置を取得できます:
local position = mouse.Hit.Position
ヒットは通常、 Tools によって、マウスの位置に向かって武器を発射します。
開発者は BasePart マウスがポイントしていることを望んでいますが、Mouse.Target を使用する必要があります。
Mouse.Hit はどう計算されますか?
ヒット 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.MouseIconEnabled。
エクスペリエンスでユーザーのマウスアイコンを取得/設定するには、UserInputService.MouseIcon を使用する必要があります。Mouse.Icon は、プラグインの新しい API がリリースされた後にデプレートされます。
カーソルのデザイン
次のガイドラインは、自分のマウスカーソルを作成するときに役立つかもしれません:
- 使用されている画像のサイズは、カーソルのサイズを決定します。
- 画像の 中央 は、マウスの入力が発行される場所です。
- デフォルトのマウス画像は 64x64 ピクセルで、マウスは 17x24 ピクセルのスペースを占有します。
System Cursors for PluginMouse
Class.PluginMouse を使用すると、Plugin:GetMouse() から取
<tbody><tr><td><img src="../../../assets/legacy/Mouse-Icon-Pointer.png"></img></td><td><code>rbxasset://SystemCursors/Arrow</code></td><td>デフォルトのクリックと選択。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-PointingHand.png"></img></td><td><code>rbxasset://SystemCursors/PointingHand</code></td><td>有効なリンク/ボタンの上にマウスポインタを置く。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-OpenHand.png"></img></td><td><code>rbxasset://SystemCursors/OpenHand</code></td><td>ドラッグ可能なアイテムの上にマウスポインタを置く。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-GrabbingHand.png"></img></td><td><code>rbxasset://SystemCursors/ClosedHand ]</code></td><td>アイテムをドラッグしています。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-IBeam.png"></img></td><td><code>rbxasset://SystemCursors/IBeam</code></td><td>テキストフィールドにカーソルを置く。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-ResizeNS.png"></img></td><td><code>rbxasset://SystemCursors/SizeNS</code></td><td>上下方向のハンドルをホバーする。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-ResizeEW.png"></img></td><td><code>rbxasset://SystemCursors/SizeEW</code></td><td>横長のサイズ変更ハンドルをホバーする。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-ResizeNESW.png"></img></td><td><code>rbxasset://SystemCursors/SizeNESW</code></td><td>コーナーのハンドルをホバーオーバーしてサイズを変更します。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-ResizeNWSE.png"></img></td><td><code>rbxasset://SystemCursors/SizeNWSE</code></td><td>コーナーのハンドルをホバーオーバーしてサイズを変更します。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-ResizeAll.png"></img></td><td><code>rbxasset://SystemCursors/SizeAll</code></td><td>複数の方向に対応するサイズ変更ハンドルの上にマウスポイント。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-ResizeSplitV.png"></img></td><td><code>rbxasset://SystemCursors/SplitNS</code></td><td>上下方向の「スプリット」ハンドルをホバーする。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-ResizeSplitH.png"></img></td><td><code>rbxasset://SystemCursors/SplitEW</code></td><td>横の "スプリット" ハンドルの上にマウスポイント。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-Forbidden.png"></img></td><td><code>rbxasset://SystemCursors/Forbidden</code></td><td>ロック/禁止されたアイテムの上にマウスポインタを置く。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-Wait.png"></img></td><td><code>rbxasset://SystemCursors/Wait</code></td><td>アクションが進行中と表示します。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-Busy.png"></img></td><td><code>rbxasset://SystemCursors/Busy ]</code></td><td>システムが忙しいことを示す。</td></tr><tr><td><img src="../../../assets/legacy/Mouse-Icon-Crosshair.png"></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-- 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 です。
注:
- Class.Mouse.TargetFilter が設定する定されている場合、ターゲットフィルターとその子孫は無視されます。
- マウスが BasePart を指していない場合、例えば空を指している場合、ターゲットは nulo になります。
- 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 の計算中にマウスがオブジェクトを無視するように設定します。オブジェクトの子孫も無視されるので、このプロパティを使用し
このプロパティは、Instance または nil に設定できます。たとえば:
local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()mouse.TargetFilter = workspace.Model
注意、Character の Players.LocalPlayer は、マウスで自動的に無視されます。
TargetSurface
このプロパティは、マウスがポイントしている Enum.NormalId 表面の BasePart を指します。このプロパティは、マウスの世界位置 ( Mouse.Hit ) と、マウスがポイントしている部分 ( 2>Class.Mouse.Target2> ) から得られます。
このプロパティは、マウスがパーツに向いていないとき、たとえばマウスが天に向いているときには意味がありません。現在、このプロパティは、これらの場合、「右」に設定されています。このプロパティを使用する前に、マウスのターゲットが 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 の Class.Camera.CFrame|CFrame から起源します。すべてのユニットレイは、2>Class.Workspace.CurrentCamera2>
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 またはClass.User
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 またはClass.User
Y プロパティは、マウスの位置の屏の垂直部分を記述します。位置は、トップレフトコーナーの下のトップバーの下に測定されます。このプロパティは Mouse.X と Vector2 を組み合わせて、マウスの位置を表示する Datatype.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
ボタン1Down は、プレイヤーが左マウスボタンを押すときにも発動します。
これは、Tool からもアクセスできます。たとえば、LocalScript に配置されると、左マウスボタンが押されるたびにコードの下のボタン1Down が印刷されます:
local Tool = script.Parent --これがツールオブジェクトであることを確認してください
Tool.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
print("Button1Down")
end)
end)
開発者は、世界スペース内のマウスの位置を確認し、BasePart および Mouse.Hit プロパティを使用して、マウスが指している場所を特定することができます。
マウスオブジェクトを取得する方法については、Mouse ページを参照してください。
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.Mouse オブジェクトを使用することをお勧めします。
コードサンプル
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)
Button1Up
左マウスボタンがリリースされるときに発動します。
For information on how to obtain the Mouse object, please see the Mouse page.
開発者は、BasePart および Mouse.Hit プロパティを使用して、マウスの位置を世界空間で見つけ、マウスが指している Mouse.Target にアクセスできます。
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.Mouse オブジェクトを使用することをお勧めします。
コードサンプル
The follow example changes the BasePart.BrickColor of every BasePart the player clicks and releases their mouse on.
In order for a part to change color, the player's mouse must be over the part both when the player presses their left mouse button down and when the player releases their left mouse button.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local target = nil
local function button1Down()
target = mouse.Target
end
local function button1Up()
if target == mouse.Target then
target.BrickColor = BrickColor.random()
end
end
mouse.Button1Down:Connect(button1Down)
mouse.Button1Up:Connect(button1Up)
Button2Down
ボタン2ダウンは、プレイヤーが右マウスボタンを押すときにも発動します。
これは、Tool からもアクセスできます。たとえば、LocalScript に配置されると、右マウスボタンが押されるたびにコードの下のボタン2Down が印刷されます:
local Tool = script.Parent --これがツールオブジェクトであることを確認してください
Tool.Equipped:Connect(function(Mouse)
Mouse.Button2Down:Connect(function()
print("Button2Down")
end)
end).
開発者は、世界スペース内のマウスの位置を確認し、BasePart および Mouse.Hit プロパティを使用して、マウスが指している場所を特定することができます。
マウスオブジェクトを取得する方法については、Mouse ページを参照してください。
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.Mouse オブジェクトを使用することをお勧めします。
コードサンプル
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.Button2Down:Connect(function()
if mouse.Target and mouse.Target.Parent then
mouse.Target.BrickColor = BrickColor.random()
end
end)
end)
Button2Up
右マウスボタンがリリースされると発射されます。
mouse.Button2Up:Connect(function()
print("button 2 up!")
end
For information on how to obtain the Mouse object, please see the Mouse page.
開発者は、BasePart および Mouse.Hit プロパティを使用して、マウスの位置を世界空間で見つけ、マウスが指している Mouse.Target にアクセスできます。
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.Mouse オブジェクトを使用することをお勧めします。
コードサンプル
The follow example changes the BasePart.BrickColor of every BasePart the player clicks and releases their right mouse button on.
In order for a part to change color, the player's mouse must be over the part both when the player presses their right mouse button down and when the player releases their right mouse button.
local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
local target = nil
mouse.Button2Down:Connect(function()
target = mouse.Target
end)
mouse.Button2Up:Connect(function()
if target == mouse.Target then
target.BrickColor = BrickColor.random()
end
end)
Idle
マウスが別のマウスイベントに渡されていない間に、心拍数があるたびに発生します。
注意、このイベントはマウスがまだ止まっているかを判断するのに使用してはなりません。心拍数が発生するたびに、それは Mouse.Move イベントの間で発生します。
For information on how to obtain the Mouse object, please see the Mouse page.
開発者は、BasePart および Mouse.Hit プロパティを使用して、マウスの位置を世界空間で見つけ、マウスが指している Mouse.Target にアクセスできます。
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.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 object, please see the Mouse page.
開発者は、BasePart および Mouse.Hit プロパティを使用して、マウスの位置を世界空間で見つけ、マウスが指している Mouse.Target にアクセスできます。
mouse.Move:Connect(function()
local position = mouse.Hit.p
local target = mouse.Target
print(target, position)
end)
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.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 object, please see the Mouse page.
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.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 object, please see the Mouse page.
注:開発者は、新しいワークで Mouse オブジェクトではなく、 Class.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)