Mouse
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
เมาส์ ถูกแทนที่โดย UserInputService และ ContextActionService ซึ่งครอบคลุมขอบเขตที่กว้างขึ้นมีคุณสมบัติมากขึ้นและสนับสนุนรูปแบบ ข้ามแพลตฟอร์ม ได้ดียิ่งขึ้นยังคงได้รับการสนับสนุนเนื่องจากการใช้งานอย่างแพร่หลาย แต่คุณควรพิจารณาใช้ทางเลือกเหล่านี้อย่างแข็งขัน
วัตถุ เมาส์ มี API หลายอย่างสำหรับจุดชี้ โดยเฉพาะอย่างยิ่งสำหรับปุ่มและการโจมตีรังสีสามารถเข้าถึงได้ผ่าน Player:GetMouse() โทรไปที่ Players.LocalPlayer ใน LocalScriptมันยังผ่านโดยอีเวนต์ Tool.Equipped
- เป็นที่สังเกตเห็นมากที่สุดสำหรับคุณสมบัติ Icon ซึ่งเปลี่ยนรูปลักษณ์ของเคอร์เซอร์
- มันออกอากาศตําแหน่งเมาส์หน้าจออย่างต่อเนื่องในโลก 3D โดยใช้คุณสมบัติ TargetFilter เก็บผลลัพธ์ของการออกอากาศรังสีใน Hit , Target และ TargetSurface คุณสมบัติสิ่งเหล่านี้สามารถมีประโยชน์สำหรับกรณีง่ายๆ แต่ WorldRoot:Raycast() ควรใช้ในสถานการณ์ raycasting ที่ซับซ้อนมากขึ้น
-- จากสคริปท้องถิ่น: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
ID เนื้อหาของภาพที่ใช้เป็นไอคอน Mouse
A CFrame ตำแหน่งที่ Workspace.CurrentCamera และสอดคล้องกับตำแหน่ง 3D ของเมาส์
วัตถุในพื้นที่ 3D ที่ mouse ชี้ไปที่
กำหนดวัตถุ (และบุตรหลานของมัน) ที่จะถูกเพิกเฉยเมื่อกำหนด Mouse.Hit และ Mouse.Target
บ่งบอกถึง 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 เป็น false
สำหรับการรับ/การตั้งค่าไอคอนเมาส์ของผู้ใช้ในประสบการณ์คุณควรใช้ Class.UserInputService.MouseIcon``Class.Mouse.Icon จะถูกละทิ้งหลังจากเปิดตัว API ใหม่สำหรับปลั๊กอินเพื่อตั้งเคอร์เซอร์เมาส์
ออกแบบเคอร์เซอร์
คำแนะนำต่อไปนี้อาจเป็นประโยชน์เมื่อสร้างเคอร์เซอร์เอง:
- ขนาดของภาพที่ใช้กำหนดขนาดของเคอร์เซอร์
- ศูนย์ กลาง ของภาพเป็นที่ที่การใส่ข้อมูลของเมาส์ถูกออก
- ภาพเมาส์เริ่มต้นมีขนาด 64x64 พิกเซล โดยเมาส์ใช้พื้นที่ 17x24 พิกเซล
เคอร์เซอร์ระบบ
เมื่อใช้ PluginMouse ที่ดึงมาจาก Plugin:GetMouse() คุณสามารถใช้ไอคอนต่อไปนี้ที่คล้ายกับเคอร์เซอร์เริ่มต้นของระบบของคุณ เช่น มือ ลูกศร เส้น I ฯลฯคุณสามารถใช้สิ่งเหล่านี้กับเหตุการณ์ GUI เช่น MouseEnter , MouseLeave และ MouseButton1Down เพื่อให้ประสบการณ์ Studio สอดคล้องเมื่อโต้ตอบกับชนิดของส่วนประกอบ 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
คุณสมบัติที่มาจากต้นกำเนิด เป็นคุณสมบัติที่บ่งบอกถึงที่ที่เมาส์เริ่มต้นมาจากมันตั้งอยู่ที่ Workspace.CurrentCamera และสอดคล้องกับตำแหน่ง 3D ของเมาส์
Mouse.UnitRay เริ่มต้นที่ตำแหน่งเดียวกับต้นกำเนิดและขยายไปตามแนวเดียวกันสําหรับสตัดในเดียวกัน
local unitRay = mouse.UnitRaylocal origin = mouse.Origin-- หน่วยเรย์.ทิศทาง = ต้นกำเนิด.p-- 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 ตัวอย่างเช่นเมื่อมันชี้ไปที่ท้องฟ้าเป้าหมายจะเป็น 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
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้เล่นกดปุ่มเมาส์ซ้ายของตนโปรดทราบว่าสิ่งนี้สามารถเข้าถึงได้จาก 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
อีเวนต์ WheelBackward จะเกิดขึ้นเมื่อล้อเมาส์ถูกเลื่อนกลับไปข้างหลังการใช้งานที่เป็นไปได้สำหรับอีเวนต์นี้รวมถึงการสลับเลนส์ปืนในนักยิงคนแรก (ยิงมุมมองบุคคลที่หนึ่ง) หรือซูมกล้องของผู้เล่น
สิ่งนี้สามารถใช้ร่วมกับอีเวนต์การเลื่อนไปข้างหน้า, 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
อีเวนต์ล้อข้างหน้าจะเกิดขึ้นเมื่อล้อเมาส์ถูกเลื่อนไปข้างหน้าการใช้งานที่เป็นไปได้สำหรับอีเวนต์นี้รวมถึงการสลับเลนส์ปืนในนักยิงคนแรก (ยิงมุมมองบุคคลที่หนึ่ง) หรือซูมกล้องของผู้เล่น
สิ่งนี้สามารถใช้ร่วมกับอีเวนต์การเลื่อนกลับไปข้างหลัง 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)