Mouse
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
Mouse è stato sostituito da UserInputService e ContextActionService, che coprono una gamma più ampia, sono più ricchi di funzionalità e supportano meglio i modelli cross-platform .Rimane supportato a causa del suo uso diffuso, ma dovresti prendere in considerazione fortemente l'uso di queste alternative.
L'oggetto Mouse ospita diverse API per i puntatori, principalmente per i pulsanti e il raycasting.Può essere accessato attraverso Player:GetMouse() chiamato sul Players.LocalPlayer in un LocalScript .Viene anche passato dall'evento Tool.Equipped .
- È più notevole per la proprietà Icon che cambia l'aspetto del cursore.
- Continua a lanciare la posizione del mouse dello schermo nel mondo 3D utilizzando la proprietà TargetFilter, memorizzando i risultati del lancio del raycast nelle proprietà Hit, Target e TargetSurface.Questi possono essere utili per casi semplici, ma WorldRoot:Raycast() dovrebbero essere utilizzati in scenari più complicati di raycasting.
- Plugins può utilizzare Plugin:GetMouse() per ottenere un PluginMouse, che si comporta in modo simile.
-- Da uno script locale:local Players = game:GetService("Players")local player = Players.LocalPlayerlocal mouse = player:GetMouse()-- Impostare l'icona del mousemouse.Icon = "rbxasset://SystemCursors/Wait"
Nota:
Questo oggetto non controlla/restringe il movimento del puntatore. Per questo, vedi UserInputService.MouseBehavior e UserInputService.MouseDeltaSensitivity .
Se due funzioni sono connesse allo stesso evento di input, come , entrambe le funzioni verranno eseguite quando l'evento si attiva.Non esiste il concetto di input che affonda/passa, poiché gli eventi non supportano questo comportamento.Tuttavia, ContextActionService ha questo comportamento attraverso BindAction .
Mentre un mouse potrebbe non essere disponibile su tutte le piattaforme, il mouse funzionerà ancora su mobile (touch) e console (gamepad), che di solito non hanno mouse o puntatori hardware.Per i comportamenti cross-piattaforma espliciti, usa UserInputService e ContextActionService .
Vedi Input e fotocamera per ulteriori informazioni sulla personalizzazione degli input nella tua esperienza.
Sommario
Proprietà
Il CFrame della posizione del mouse nello spazio 3D.
L'ID del contenuto dell'immagine utilizzata come icona Mouse.
Un CFrame posizionato al Workspace.CurrentCamera e orientato verso la posizione 3D del mouse.
L'oggetto nello spazio 3D a cui il mouse punta.
Determina un oggetto (e i suoi discendenti) da ignorare quando si determina Mouse.Hit e Mouse.Target .
Indica il Enum.NormalId della superficie BasePart in cui il mouse è puntato.
Un Ray diretto verso la posizione del mondo del mouse, originato dalla posizione del mondo Workspace.CurrentCamera.
Descrive la larghezza della finestra di gioco in pixel.
Descrive l'altezza della finestra del gioco in pixel.
Descrive la componente X (orizzontale) della posizione del mouse sullo schermo.
Descrive la componente Y (verticale) della posizione dello schermo del mouse.
Metodi
Eventi
Si accende quando viene premuto il pulsante sinistro del mouse.
Si accende quando viene rilasciato il pulsante sinistro del mouse.
Si accende quando viene premuto il pulsante destro del mouse.
Sparato quando viene rilasciato il pulsante destro del mouse.
Sparato durante ogni battito di cuore in cui il mouse non viene passato ad un altro evento del mouse.
Sparato quando il mouse viene spostato.
Si accende quando la ruota del mouse viene scoraggiata all'indietro.
Si accende quando la ruota del mouse viene scorruta in avanti.
Proprietà
Hit
Campioni di codice
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)
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
Campioni di codice
local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
mouse.Icon = "http://www.roblox.com/asset?id=163023520"
Origin
Campioni di codice
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
Campioni di codice
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
TargetSurface
Campioni di codice
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
ViewSizeX
Campioni di codice
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
Campioni di codice
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)
Campioni di codice
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)
Campioni di codice
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)
Metodi
Eventi
Button1Down
Button1Up
Button2Down
Button2Up
Idle
Campioni di codice
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
Campioni di codice
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
Campioni di codice
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
Campioni di codice
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)