Mouse
*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.
Mouse telah digantikan oleh UserInputService dan ContextActionService , yang mencakup cakupan yang lebih luas, lebih kaya fitur, dan mendukung pola silang platform lebih baik.Ini tetap didukung karena penggunaannya yang luas, tetapi Anda harus sangat mempertimbangkan menggunakan alternatif ini.
Objek Tikus memiliki berbagai API untuk pointer, terutama untuk tombol dan raycasting.Ini dapat diakses melalui Player:GetMouse() dipanggil pada Players.LocalPlayer di dalam LocalScript .Ini juga dilewati oleh acara Tool.Equipped .
- Ini paling terkenal untuk properti Icon , yang mengubah penampilan kurator.
- Ini terus melemparkan posisi mouse layar ke dunia 3D menggunakan properti TargetFilter , menyimpan hasil raycast di Hit , Target , dan TargetSurface properti.Ini bisa berguna untuk kasus sederhana, tetapi WorldRoot:Raycast() harus digunakan dalam skenario raycasting yang lebih rumit.
- Plugins dapat menggunakan Plugin:GetMouse() untuk mendapatkan PluginMouse , yang berperilaku serupa.
-- Dari LocalScript:local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- Mengatur ikon mousemouse.Icon = "rbxasset://SystemCursors/Wait"
Catatan:
Objek ini tidak mengontrol/membatasi gerakan pointer. Untuk ini, lihat UserInputService.MouseBehavior dan UserInputService.MouseDeltaSensitivity .
Jika dua fungsi terhubung ke peristiwa input yang sama, seperti , kedua fungsi akan berjalan saat peristiwa terjadi.Tidak ada konsep sinkronisasi/pengiriman input, karena acara tidak mendukung perilaku ini.Namun, ContextActionService memiliki perilaku ini melalui BindAction .
Sementara mouse mungkin tidak tersedia di semua platform, Mouse masih akan berfungsi di perangkat seluler (sentuh) dan konsol (gamepad), yang biasanya tidak memiliki perangkat mouse atau penunjuk.Untuk perilaku silang platform eksplisit, gunakan UserInputService dan ContextActionService.
Lihat Input dan Kamera untuk informasi lebih lanjut tentang menyesuaikan input dalam pengalaman Anda.
Rangkuman
Properti
The CFrame dari posisi mouse di ruang 3D.
ID konten dari gambar yang digunakan sebagai ikon Mouse.
A CFrame ditempatkan di Workspace.CurrentCamera dan berorientasi pada posisi 3D mouse.
Objek di ruang 3D yang ditunjuk ke mouse adalah menunjuk ke.
Menentukan objek (dan keturunannya) untuk diabaikan saat menentukan Mouse.Hit dan Mouse.Target .
Menunjukkan Enum.NormalId permukaan BasePart di mana mouse ditujukan.
A Ray ditujukan ke posisi dunia mouse, berasal dari posisi dunia Workspace.CurrentCamera.
Menggambarkan lebar jendela permainan dalam piksel.
Menggambarkan ketinggian jendela permainan dalam piksel.
Menggambarkan komponen X (horizontal) dari posisi mouse di layar.
Menggambarkan komponen Y (vertikal) dari posisi layar mouse.
Acara
Melepaskan api saat tombol mouse kiri ditekan.
Melepaskan api saat tombol mouse kiri dilepaskan.
Melepaskan api saat tombol mouse kanan ditekan.
Ditembak ketika tombol mouse kanan dilepaskan.
Ditembak selama setiap detak jantung yang mouse tidak dikirim ke acara mouse lain.
Ditembak ketika mouse dipindahkan.
Melepaskan api saat roda mouse diputar ke belakang.
Melepaskan api saat roda mouse diputar ke depan.
Properti
Hit
Properti ini menunjukkan CFrame dari posisi mouse di ruang 3D. Perhatikan bahwa Mouse.TargetFilter dan keturunannnya akan diabaikan.
Pengembang dapat mendapatkan posisi Hit seperti ini:
local position = mouse.Hit.Position
Hit sering digunakan oleh Tools untuk menembak senjata ke arah mouse dalam orang ketiga.
Pengembang yang mencari BasePart mouse yang ditunjukkan harus menggunakan Mouse.Target .
Bagaimana Mouse.Hit dihitung?
Posisi Hit CFrame dihitung sebagai titik intersepsi antara internal mouse Ray (versi yang diperpanjang dari Mouse.UnitRay) dan objek di ruang 3D (seperti bagian).
Orientasi CFrame pukul sesuai dengan arah Mouse.UnitRay .
local unitRayDirection = mouse.UnitRay.Directionlocal mouseHitDirection = mouse.Hit.lookVector-- unitRayDirection ≈ mouseHitDirection-- the vectors are approximately equal
Catatan, gulungan dari Workspace.CurrentCamera tidak digunakan saat menghitung orientasi Hit CFrame .
Sinar internal mouse memanjang untuk 1.000 stud.Jika mouse tidak menunjuk ke objek di ruang 3D (misalnya saat menunjuk ke langit), properti ini akan berjarak 1.000 stud dari Workspace.CurrentCamera .
Contoh Kode
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
Simbol adalah properti yang menentukan gambar yang digunakan sebagai penunjuk.Jika kosong, panah default digunakan.Sementara kursor mengarah ke atas GuiButton , properti ini diabaikan sementara.
Untuk menyembunyikan kursor sepenuhnya, jangan menggunakan gambar transparan - sebagai gantinya, atur UserInputService.MouseIconEnabled ke false.
Untuk mendapatkan/menetapkan ikon mouse pengguna di pengalaman, Anda harus menggunakan UserInputService.MouseIcon . Mouse.Icon akan dihapus setelah API baru untuk plugin dirilis untuk mengatur kursor mouse.
Mendesain Kursor
Panduan berikut dapat terbukti berguna saat membuat kursor mouse Anda sendiri:
- Dimensi gambar yang digunakan menentukan ukuran kursor.
- Pusat dari gambar adalah tempat di mana input mouse dikeluarkan.
- Gambar mouse default adalah 64x64 piksel, dengan mouse mengambil 17x24 piksel ruang.
Kursor Sistem
Saat menggunakan PluginMouse yang diambil dari Plugin:GetMouse() , Anda dapat menggunakan ikon berikut yang mirip dengan kurator default sistem Anda, seperti tangan, panah, I-beam, dll.Anda dapat menggunakan ini dengan acara GUI seperti MouseEnter , MouseLeave , dan MouseButton1Down untuk memberikan pengalaman Studio yang konsisten saat berinteraksi dengan jenis komponen GUI tertentu.Perhatikan bahwa ini hanya berfungsi untuk plugin Studio; mereka tidak akan berfungsi untuk objek lain Mouse .
<th>Aset</th><th>Penggunaan yang disarankan</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>Klik dan seleksi default.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-PointingHand.png" width="30"></img></td><td><code>rbxasset://SystemCursors/PointingHand</code></td><td>Melayang di atas tautan/tombol aktif.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-OpenHand.png" width="30"></img></td><td><code>rbxasset://SystemCursors/OpenHand</code></td><td>Melayang di atas item yang dapat diseret.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-GrabbingHand.png" width="30"></img></td><td><code>rbxasset://SystemCursors/ClosedHand</code></td><td>Menyeret item.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-IBeam.png" width="30"></img></td><td><code>rbxasset://SystemCursors/IBeam</code></td><td>Melayang di bidang teks.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeNS.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeNS</code></td><td>Melayang di atas pegangan resize vertikal.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeEW.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeEW</code></td><td>Melayang di atas pegangan resize horizontal.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeNESW.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeNESW</code></td><td>Melayang di atas sisi penangan resize.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeNWSE.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeNWSE</code></td><td>Melayang di atas sisi pegangan resize.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeAll.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SizeAll</code></td><td>Melayang di atas pegangan resize multi-arah.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeSplitV.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SplitNS</code></td><td>Melayang di atas pegangan "split" vertikal.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-ResizeSplitH.png" width="30"></img></td><td><code>rbxasset://SystemCursors/SplitEW</code></td><td>Menggantung di atas pegangan "split" horizontal.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Forbidden.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Forbidden</code></td><td>Melayang di atas item terkunci/dilarang.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Wait.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Wait</code></td><td>Menunjukkan bahwa tindakan sedang berlangsung.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Busy.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Busy</code></td><td>Menunjukkan sistem sibuk.</td></tr><tr><td><img src="../../../assets/misc/Mouse-Icon-Crosshair.png" width="30"></img></td><td><code>rbxasset://SystemCursors/Cross</code></td><td>Melayang di atas area seleksi pinpoint.</td></tr></tbody>
Lihat* |
---|
Contoh Kode
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
Properti asal menunjukkan dari mana asal mouse berasal.Ini ditempatkan di Workspace.CurrentCamera dan diposisikan ke arah posisi 3D mouse.
Mouse.UnitRay dimulai di posisi yang sama dengan Asal, dan berlanjut untuk satu stud di arah yang sama.
local unitRay = mouse.UnitRaylocal origin = mouse.Origin-- unitRay.Direction = asal.p-- unitRay.Direction ≈ origin.lookVector
Untuk posisi Mouse di ruang 3D, lihat Mouse.Hit .
Contoh Kode
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
Objek di ruang 3D yang ditunjuk ke mouse adalah menunjuk ke.
Catatan:
- Jika Mouse.TargetFilter telah atur, filter target dan keturunannya akan diabaikan.
- Ketika mouse tidak menunjuk pada BasePart, misalnya ketika menunjuk ke langit, Target akan menjadi nil .
- Pengembang yang mencari posisi mouse di ruang 3D harus menggunakan Mouse.Hit .
Contoh Kode
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
Properti ini menentukan objek yang diabaikan oleh mouse saat menghitung Mouse.Hit dan Mouse.Target .Keturunan objek juga diabaikan, sehingga mungkin untuk mengabaikan beberapa objek selama mereka adalah keturunan dari objek ke mana properti ini atur.Properti ini berguna saat menyaring model yang berisi efek khusus atau dekorasi yang tidak boleh mempengaruhi Mouse.Hit atau Mouse.Target .
Properti ini dapat ditetapkan ke Instance atau nil, misalnya:
local Players = game:GetService("Players")local Workspace = game:GetService("Workspace")local player = Players.LocalPlayerlocal mouse = player:GetMouse()mouse.TargetFilter = Workspace.Model
Perhatikan bahwa Character dari Players.LocalPlayer diabaikan oleh mouse secara otomatis.
TargetSurface
Properti ini menunjukkan Enum.NormalId permukaan BasePart di mana mouse ditujukan.Properti ini berasal dari posisi dunia mouse ( Mouse.Hit ) dan bagian yang dituju oleh mouse ( Mouse.Target ).
Properti ini tidak berarti ketika mouse tidak menunjuk ke bagian, misalnya ketika mouse menunjuk ke langit.Saat ini, properti ini diatur ke 'Kanan' dalam kondisi ini.Sebelum menggunakan properti ini, periksa apakah target mouse tidak nil .
local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- Periksa apakah ada bagian di mana mouse ditunjukif 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
Contoh Kode
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
Properti UnitRay adalah Ray ditujukan ke posisi mouse di ruang 3D (dijelaskan oleh Mouse.Hit ).Asalnya dari CFrame dari Workspace.CurrentCamera .Seperti semua sinar unit, ia memiliki jarak 1.
local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()print(mouse.UnitRay.Direction.Magnitude) -- Always 1
ViewSizeX
Properti ViewSizeX menggambarkan komponen horizontal dari ukuran jendela game dalam piksel.
Contoh Kode
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
Properti ViewSizeY menggambarkan komponen vertikal dari ukuran jendela permainan dalam piksel. Panjang ini termasuk ruang yang digunakan oleh bilah atas.
Contoh Kode
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)
Saat mendeteksi perubahan posisi mouse di layar, disarankan agar Anda menggunakan ContextActionService:BindAction() dengan Enum.UserInputType.MouseMovement atau UserInputService.InputChanged, yang keduanya menggambarkan posisi mouse menggunakan Position (a Vector3) dari InputObject, bukan menggunakan ini dan properti terkait.
Properti X menggambarkan komponen horizontal posisi mouse di layar.Posisi diukur dalam piksel relatif terhadap sudut kiri atas, di bawah bilah atas.Properti ini dapat digunakan bersama dengan Mouse.Y untuk menghasilkan Vector2 yang mewakili posisi mouse:
local position = Vector2.new(mouse.X, mouse.Y)
Properti ini tidak menembak Changed atau sinyal yang dikembalikan dari GetPropertyChangedSignal . Gunakan acara Mouse.Move sebagai gantinya.
Contoh Kode
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)
Saat mendeteksi perubahan posisi mouse di layar, disarankan agar Anda menggunakan ContextActionService:BindAction() dengan Enum.UserInputType.MouseMovement atau UserInputService.InputChanged, yang keduanya menggambarkan posisi mouse menggunakan Position (a Vector3) dari InputObject, bukan menggunakan ini dan properti terkait.
Properti Y menggambarkan komponen vertikal posisi mouse di layar.Posisi diukur dalam piksel relatif terhadap sudut kiri atas, di bawah bilah atas.Properti ini dapat digunakan bersama dengan Mouse.X untuk menghasilkan Vector2 yang mewakili posisi mouse:
local position = Vector2.new(mouse.X, mouse.Y)
Properti ini tidak menembak Changed atau sinyal yang dikembalikan dari GetPropertyChangedSignal . Gunakan acara Mouse.Move sebagai gantinya.
Contoh Kode
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)
Metode
Acara
Button1Down
Acara ini terjadi ketika pemain menekan tombol mouse kiri mereka.Perhatikan bahwa ini dapat diakses dari Tool ; misalnya, saat ditempatkan di LocalScript , kode di bawah ini akan dicetak Button1Down setiap kali tombol mouse kiri ditekan.
local tool = script.Parent -- Pastikan ini adalah objek Alat
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
print("Button1Down")
end)
end)
Anda dapat mengetahui posisi mouse di ruang dunia dan jika menunjuk ke salah satu BasePart menggunakan properti Hit dan Target.
Button1Up
Acara ini terjadi ketika pemain melepaskan tombol mouse kiri mereka.Perhatikan bahwa ini dapat diakses dari Tool ; misalnya, saat ditempatkan di LocalScript , kode di bawah ini akan dicetak Button1Up setiap kali tombol mouse kiri dilepaskan.
local tool = script.Parent -- Pastikan ini adalah objek Alat
tool.Equipped:Connect(function(mouse)
mouse.Button1Up:Connect(function()
print("Button1Up")
end)
end)
Anda dapat mengetahui posisi mouse di ruang dunia dan jika menunjuk ke salah satu BasePart menggunakan properti Hit dan Target.
Button2Down
Acara ini terjadi ketika pemain menekan tombol mouse kanan mereka.Perhatikan bahwa ini dapat diakses dari Tool ; misalnya, saat ditempatkan di LocalScript , kode di bawah ini akan dicetak Button2Down setiap kali tombol mouse kanan ditekan.
local tool = script.Parent -- Pastikan ini adalah objek Alat
tool.Equipped:Connect(function(mouse)
mouse.Button2Down:Connect(function()
print("Button2Down")
end)
end)
Anda dapat mengetahui posisi mouse di ruang dunia dan jika menunjuk ke salah satu BasePart menggunakan properti Hit dan Target.
Button2Up
Acara ini terjadi ketika pemain melepaskan tombol mouse kanan mereka.Perhatikan bahwa ini dapat diakses dari Tool ; misalnya, saat ditempatkan di LocalScript , kode di bawah ini akan dicetak Button2Up setiap kali tombol mouse kanan dilepaskan.
local tool = script.Parent -- Pastikan ini adalah objek Alat
tool.Equipped:Connect(function(mouse)
mouse.Button2Up:Connect(function()
print("Button2Up")
end)
end)
Anda dapat mengetahui posisi mouse di ruang dunia dan jika menunjuk ke salah satu BasePart menggunakan properti Hit dan Target.
Idle
Ditembak selama setiap detak jantung yang mouse tidak dikirim ke acara mouse lain.
Catatan, acara ini tidak boleh digunakan untuk menentukan kapan mouse masih. Karena menembak setiap denyut nadi, ia akan menembak di antara Mouse.Move peristiwa.
Untuk informasi tentang cara mendapatkan objek Mouse , silakan lihat halaman Mouse .
Pengembang dapat mengetahui posisi mouse di ruang dunia, dan jika menunjuk ke salah satu BasePart menggunakan properti Mouse.Hit dan Mouse.Target.
Catatan, pengembang disarankan untuk menggunakan UserInputService alih-alih objek Mouse dalam pekerjaan baru.
Contoh Kode
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
Ditembak ketika mouse dipindahkan.
Catatan, acara ini dinyalakan ketika posisi mouse diperbarui, oleh karena itu akan menyala berulang kali saat dipindahkan.
Untuk informasi tentang cara mendapatkan objek Mouse , silakan lihat halaman Mouse .
Pengembang dapat mengetahui posisi mouse di ruang dunia, dan jika menunjuk ke salah satu BasePart menggunakan properti Mouse.Hit dan Mouse.Target.
mouse.Move:Connect(function()
local position = mouse.Hit.p
local target = mouse.Target
print(target, position)
end)
Catatan, pengembang disarankan untuk menggunakan UserInputService alih-alih objek Mouse dalam pekerjaan baru.
Contoh Kode
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
Acara WheelBackward terjadi ketika roda mouse diputar ke belakang.Penggunaan yang mungkin untuk acara ini termasuk menyalakan scope senjata di penembak orang pertama (FPS) atau memperbesar kamera pemain.
Ini dapat digunakan bersama dengan acara gulir ke depan, Mouse.WheelForward .
Untuk informasi tentang cara mendapatkan objek Mouse , silakan lihat halaman Mouse .
Catatan, pengembang disarankan untuk menggunakan UserInputService alih-alih objek Mouse dalam pekerjaan baru.
Contoh Kode
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
Acara WheelForward terjadi ketika roda mouse diputar ke depan.Penggunaan yang mungkin untuk acara ini termasuk menyalakan scope senjata di penembak orang pertama (FPS) atau memperbesar kamera pemain.
Ini dapat digunakan bersama dengan acara gulir ke belakang, Mouse.WheelBackward .
Untuk informasi tentang cara mendapatkan objek Mouse , silakan lihat halaman Mouse .
Catatan, pengembang disarankan untuk menggunakan UserInputService alih-alih objek Mouse dalam pekerjaan baru.
Contoh Kode
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)