Mouse
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
เมาส์ ถูกซูเปอร์เดจโดย UserInputService และ ContextActionService ซึ่งครอบคลุมขอบเขตที่กว้างขวาง มีคุณสมบัติมากขึ้น และสนับสนุน 0>รูปแบบขนาดเครื่อ
เมาส์ วัตถุบ้านต่างๆ API สำหรับผู้ชี้ โดยเฉพาะสำหรับปุ่มและ raycasting สามารถเข้าถึงได้ผ่าน Player:GetMouse() ที่เรียกใน Class.Play
- มันเป็นที่น่าสังเกตมากสำหรับสมบัติสัญลักษณ์ Icon ซึ่งเปลี่ยนรูปลักษณ์ของเมาส์
- มันเรย์คาสต์ตำแหน่งเมาส์ของหน้าจอไปยังโลก 3D โดยใช้คุณสมบัติ TargetFilter
- Plugins สามารถใช้ Plugin:GetMouse() เพื่อรับ Class.PluginMouse ซึ่งมีพฤติกรรมเช่นกัน
-- จากโค้ดท้องถิ่น:local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- การตั้งไอคอนเมาส์mouse.Icon = "rbxasset://SystemCursors/Wait"
หมายเหตุ:
วัตถุนี้ไม่ควบคุม/จำกัดการเคลื่อนที่ของเข็มขัดประกายตา สำหรับสิ่งนี้ให้ดูที่ UserInputService.MouseBehavior และ UserInputService.MouseDeltaSensitivity
หากสองคุณสมบัติเชื่อมต่อกับเหตุการณ์เดียวกันเช่น Button1Down หรือ ทั้งสอง คุณสมบัติจะดำเนินการเมื่อเห
ขณะที่เมาส์อาจไม่สามารถใช้งานได้ทุกแพลตฟอร์ม แต่เมาส์จะทำงานได้บนโทรศัพท์ (แตะ) และคอนโซล (เกมแพด) ซึ่งไม่มีเครื่องมือเล่นเมาส์หรือเครื่องมือจุดมุ่งหมาย สำห
ดู การป้อนข้อมูลและกล้อง สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการปรับแต่งการป้อนในประสบการณ์ของคุณ
สรุป
คุณสมบัติ
ตําแหน่งของเมาส์ในพื้นที่ 3D
รหัสเนื้อหาของรูปภาพที่ใช้เป็นไอคอน Mouse
A CFrame ตั้งอยู่ที่ Workspace.CurrentCamera และมุ่งหน้าไปยังตำแหน่ง 3D ของเมาส์
วัตถุในพื้นที่ 3D ที่ mouse กำลังชี้ไป
กำหนดวัตถุ (และลูกหลานของมัน) ที่จะได้รับการเพิกเฉยเมื่อกำหนด Mouse.Hit และ Mouse.Target
ระบุ Enum.NormalId พื้นผิวที่มี BasePart ของ Class.BasePart ที่เมาส์กำลังชี้ไป
A Ray ที่ชี้ไปที่ตำแหน่งโลกของเมาส์ โดยมีต้นกำเนิดจากตำแหน่งโลกของ Workspace.CurrentCamera
อธิบายความกว้างของหน้าต่างเกมในพิกเซล
อธิบายความสูงของหน้าต่างเกมในพิกเซล
อธิบายส่วน X (แนวตั้ง) ของตำแหน่งเมาส์บนหน้าจอ
อธิบายส่วนประกอบ Y (vertical) ของตำแหน่งหน้าจอ
อีเวนต์
ไฟร์เมื่อปุ่มเมาส์ซ้ายถูกกด
เกิดขึ้นเมื่อปุ่มเมาส์ซ้ายถูกปล่อย
เกิดขึ้นเมื่อปุ่มเมาส์ขวาถูกกด
ปล่อยเมื่อปุ่มเมาส์ขวาถูกปล่อย
เกิดขึ้นในทุกๆ หัวใจที่เมาส์ไม่ได้ถูกส่งไปยังอีเมนต์เมาส์อื่น
ไฟร์เมื่อเมาส์ถูกย้าย
กระตุ้นเมื่อล้อเมาส์ถูกเลื่อนกลับ
เกิดขึ้นเมื่อล้อเมาส์เลื่อนไปข้างหน้า
คุณสมบัติ
Hit
สมบัตินี้แสดงตำแหน่งของเมาส์ในพื้นที่ 3D โดยใช้ CFrame ของเมาส์ หมายเหตุว่า Mouse.TargetFilter และบุตรหลานของมันจะถูกเพิกเฉย
ผู้พัฒนาสามารถรับตำแหน่งของการโจมตีเช่น:
local position = mouse.Hit.Position
การโจมตีมักใช้โดย Tools เพื่อยิงอาวุธไปยังเมาส์ในบุคคลที่สาม
ผู้พัฒนากำลังมองหา BasePart เมาส์กำลังชี้ไปที่คุณควรใช้ Mouse.Target
Mouse.Hit จะคำนวณอย่างไร?
ตําแหน่งของ Hit CFrame จะคํานวณเป็นจุดสัมพันธ์ระหว่างเมาส์ภายใน Ray (ฉบับขยายของ Mouse.UnitRay ) และวัตถุในพื้นที่ 3D (เช่น ส่วน)
ตำแหน่งของ Hit CFrame ตรงกับทิศทางของ Mouse.UnitRay
local unitRayDirection = mouse.UnitRay.Directionlocal mouseHitDirection = mouse.Hit.lookVector-- unitRayDirection ≈ mouseHitDirection-- the vectors are approximately equal
หมายเหตุ, การเลื่อนของ Workspace.CurrentCamera ไม่ได้ใช้เมื่อคำนวณตำแหน่งของ Hit CFrame
ลำแสงภายในของเมาส์ยืดออกไป 1,000 หน่วย หากเมาส์ไม่ได้ชี้ไปที่วัตถุในพื้นที่ 3D (เช่นเมื่อชี้ไปที่ท้องฟ้า) ลักษณะนี้จะอยู่ห่างออกไป 1,000 หน่วยจาก Workspace.CurrentCamera
ตัวอย่างโค้ด
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
สำหรับการรับ/การตั้งไอคอนผู้ใช้ในประสบการณ์คุณควรใช้ Class.UserInputService.MouseIcon``Class.Mouse.Icon จะถูกยกเลิกการใช้งานหลังจากเปิดตัว API ใหม่สำหรับปลั๊กอินเพื่อตั้งไอคอนผู้ใช้
การออกแบบ Cursor
คู่มือต่อไปนี้อาจเป็นประโยชน์ในการสร้างเมาส์ของคุณเอง:
- ขนาดของรูปภาพที่ใช้กำหนดขนาดของตัวเลือก
- ศูนย์กลางของรูปภาพคือที่ที่การป้อนเมาส์จะออก
- รูปภาพเมาส์เริ่มต้นคือ 64x64 พิกเซล, มีเมาส์ 17x24 พิกเซล
System Cursors for PluginMouse
เมื่อใช้ 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 และมุ่งหน้าไปทางด้านข้างของเมาส์
Mouse.UnitRay เริ่มต้นที่ตำแหน่งเดียวกับต้นไม้ และขยายไปในทิศทางเดียวกัน
local unitRay = mouse.UnitRaylocal origin = mouse.Origin-- unitRay.Direction = ค่าเริ่มต้น-- unitRay.Direction ≈ origin.lookVector
สำหรับตำแหน่งของ Mouse ในพื้นที่ 3D ให้ดูที่ 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 เช่นเมื่อมันชี้ไปที่ท้องฟ้าเป้าหมายจะเป็น zero
- ผู้พัฒนาที่กำลังมองหาตำแหน่งของเมาส์ในพื้นที่ 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 ) และส่วนที่เมาส์กำลังชี้ไป ( <
สมบัติสมุดนี้ไม่มีค่าเมื่อเมาส์ไม่ได้ชี้ไปที่ส่วนหนึ่ง เช่นเมื่อเมาส์ชี้ไปที่ท้องฟ้า ในขณะนี้ สมบัติสมุดนี้กำหนดไว้ที่ 'ขวา' ภายใต้เหตุผลเหล่านี้ ก่อนการใช้สมบัติสมุดนี้ ตรว
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 ของ <
local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()print(mouse.UnitRay.Direction.magnitude) -- Always 1
ViewSizeX
สมบัติขนาด X อธิบายคอมพอนเทนต์แนวตั้งของขนาดหน้าต่างเกมในพิกเซล
ตัวอย่างโค้ด
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
สมบัติสมุดขนาดของมุมมอง ระบุขนาดของตัวอักษรเกมในพิกเซล ความยาวนี้รวมถึงพื้นที่ที่ใช้โดยแถบด้านบน
ตัวอย่างโค้ด
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() พร
สมบัติของ X อธิบายคอมพอนเนนต์แนวตั้งของตําแหน่งเมาส์บนหน้าจอ ตําแหน่งถูกวัดในพิกเซลเมื่อเปรียบเทียบกับมุมด้านบนซ้าย ใต้มุมด้านล่างซ้าย สมบัตินี
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() พร
สมบัติของ Y อธิบายส่วนประกอบแนวตั้งของตำแหน่งเมาส์บนหน้าจอ ตำแหน่งถูกวัดในพิกเซลที่เป็นสัดเขตด้านบนซ้าย ใต้แถบด้านบน สมบัตินี้สามารถใช้ร่วมกับ Class
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
ปุ่ม1ลงยังเปิดให้ใช้งานเมื่อผู้เล่นกดปุ่มเมาส์ซ้าย
นอกจากนี้ยังสามารถเข้าถึงได้จาก Tool เช่นเดียวกัน เมื่อวางใน LocalScript จะพิมพ์ปุ่ม1ลงเมื่อปุ่มเมาส์ซ้ายถูกกด:
local Tool = script.Parent --ตรวจสอบว่านี่เป็นวัตถุเครื่องมือ
Tool.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
print("Button1Down")
end)
end)
ผู้พัฒนาสามารถดูตำแหน่งของเมาส์ในพื้นที่โลกได้ และหากมันชี้ไปที่ BasePart ใด ๆ โดยใช้สมบัติสมบัติ Mouse.Hit และ Mouse.Target
สำหรับข้อมูลเกี่ยวกับวิธีการรับตัวเองดาวเมาส์โปรดดูหน้า Mouse
หมายเหตุ, ผู้พัฒนาควรใช้ UserInputService แทน 5> 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
เกิดขึ้นเมื่อปุ่มเมาส์ซ้ายถูกปล่อย
สำหรับข้อมูลเกี่ยวกับวิธีการรับ Mouse วัตถุโปรดดูหน้า Mouse
ผู้พัฒนาสามารถดูตำแหน่งของเมาส์ในพื้นที่โลกได้และหากมันชี้ไปที่ใด ๆ BasePart โดยใช้คุณสมบัติ Mouse.Hit และ Mouse.Target
หมายเหตุ, ผู้พัฒนาควรใช้ UserInputService แทน 5> 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
Button2Down จะยิงแม้ว่าผู้เล่นจะกดปุ่มเมาส์ขวา
นอกจากนี้ยังสามารถเข้าถึงได้จาก Tool เช่นเดียวกัน เมื่อวางใน LocalScript จะพิมพ์ปุ่มด้านล่างเมื่อปุ่มเมาส์ที่เหมาะสมถูกกด:
local Tool = script.Parent --ตรวจสอบว่านี่เป็นวัตถุเครื่องมือ
Tool.Equipped:Connect(function(Mouse)
Mouse.Button2Down:Connect(function()
print("Button2Down")
end)
end).
ผู้พัฒนาสามารถดูตำแหน่งของเมาส์ในพื้นที่โลกได้ และหากมันชี้ไปที่ BasePart ใด ๆ โดยใช้สมบัติสมบัติ Mouse.Hit และ Mouse.Target
สำหรับข้อมูลเกี่ยวกับวิธีการรับตัวเองดาวเมาส์โปรดดูหน้า Mouse
หมายเหตุ, ผู้พัฒนาควรใช้ UserInputService แทน 5> 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
สำหรับข้อมูลเกี่ยวกับวิธีการรับ Mouse วัตถุโปรดดูหน้า Mouse
ผู้พัฒนาสามารถดูตำแหน่งของเมาส์ในพื้นที่โลกได้และหากมันชี้ไปที่ใด ๆ BasePart โดยใช้คุณสมบัติ Mouse.Hit และ Mouse.Target
หมายเหตุ, ผู้พัฒนาควรใช้ UserInputService แทน 5> 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 อีเวนต์
สำหรับข้อมูลเกี่ยวกับวิธีการรับ Mouse วัตถุโปรดดูหน้า Mouse
ผู้พัฒนาสามารถดูตำแหน่งของเมาส์ในพื้นที่โลกได้และหากมันชี้ไปที่ใด ๆ BasePart โดยใช้คุณสมบัติ Mouse.Hit และ Mouse.Target
หมายเหตุ, ผู้พัฒนาควรใช้ UserInputService แทน 5> 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
ไฟร์เมื่อเมาส์ถูกย้าย
หมายเหตุ, เหตุการณ์นี้จะถูกเรียกเมื่อตำแหน่งเมาส์ได้รับการปรับปรุงดังนั้นจึงจะเรียกเหตุการณ์ซ้ำซากเมื่อเคลื่อนที่
สำหรับข้อมูลเกี่ยวกับวิธีการรับ 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 แทน 5> 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
เหตุการณ์ WheelBackward จะเกิดขึ้นเมื่อล้อเมาส์เลื่อนไปทางด้านหลัง สามารถใช้งานได้ทั้งในมุมมองของผู้ชมและมุมมองของผู้เล่น (ยิงมุมมองบุคคลที่หนึ่ง) โดยการเปลี่ยนโซเคิร์สของเหยี่ยวในช่วงเวลาการโหมดยิง
นี้สามารถใช้ร่วมกับกิจกรรมเลื่อนไปข้างหน้า, Mouse.WheelForward
สำหรับข้อมูลเกี่ยวกับวิธีการรับ Mouse วัตถุโปรดดูหน้า Mouse
หมายเหตุ, ผู้พัฒนาควรใช้ UserInputService แทน 5> 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
เหตุการณ์ WheelForward จะเกิดขึ้นเมื่อล้อเมาส์เลื่อนไปข้างหน้า การใช้งานที่อาจเกิดขึ้นสำหรับเหตุการณ์นี้ ได้แก่ การเปลี่ยนเกียร์ของปืนในผู้ชมด้านหน้า (ยิงมุมมองบุคคลที่หนึ่ง) หรือการซูมกล้องของผู้เล่น
นี่สามารถใช้ร่วมกับเหตุการณ์เลื่อนลูกศรไปยังด้านหลัง, Mouse.WheelBackward
สำหรับข้อมูลเกี่ยวกับวิธีการรับ Mouse วัตถุโปรดดูหน้า Mouse
หมายเหตุ, ผู้พัฒนาควรใช้ UserInputService แทน 5> 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)