一個 輸入對象 代表單一使用者輸入,例如滑鼠移動、觸碰、按鍵操作等。當輸入開始時創建。
這個對象的屬性會根據 UserInputType 變化。每種輸入都會經歷各種變化到其 UserInputState 。在輸入的使用期間,其他進一步描述輸入的特性可能會變更,例如 Position 和 Delta 。鍵盤和遊戲控制器按鈕將擁有 KeyCode 屬性設定。
在輸入開始時創建一個相同的對象,直到輸入結束為止持續存在並更新。因結果,當使用者變更問題的輸入時,你可以使用 Changed 事件來跟蹤對物件的變更。您也可以將這些對象放入啟用輸入列表,並在事件,例如 UserInputService.InputBegan 創建後與對象互動。這主要適用於觸摸事件,因為每個觸摸點都會有獨立的輸入對象。
也見:
- ContextActionService , 會將輸入對象傳送到 bound 行動處理功能
- UserInputService , 其事件和功能經常使用輸入對象
- GuiObject , 其事件與使用者輸入相關的輸入對象
範例程式碼
下面的例子展示了處理輸入從輸入開始依據其類型的許多使用例子之一。
處理輸入開始
-- 為了使用輸入開始事件,必須使用 UserInputService 服務
local UserInputService = game:GetService("UserInputService")
-- 提供多種用途案例的樣本功能,用於各種類型的使用者輸入
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)
概要
屬性
一個 Vector3 描述鼠標/手柄移動之間的 Delta。
包含一個枚值,描述使用的輸入類型。
描述此輸入的位置值。
描述輸入正在執行的狀態,依照特定流程進行,取決於 UserInputType 。
描述正在執行的輸入類型 (滑鼠標、鍵盤、遊戲手柄、觸摸等)。
屬性
Delta
範例程式碼
創建望遠鏡腳本
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- 在縮放前將相機重設為 CFrame 和 FieldOfView
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- 允許攝影機由腳本更改
camera.CameraType = Enum.CameraType.Scriptable
-- 在縮放前儲存相機特性
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- 縮放相機
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- 鎖定並減慢滑鼠
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- 重設縮放角度
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- 切換相機縮放/取消縮放
local function MouseClick()
if zoomed then
-- 縮放相機
ResetCamera()
else
-- 在相機放大
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- 任何更高的東西都會讓查找上下更加困難;推薦 0~1 之間的任何東西
local smoothness = 0.05 -- 推薦任何介於 0~1 之間
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end
KeyCode
範例程式碼
綁定支持遊戲控制器鍵碼
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local function actionHandler(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Action Handler: " .. actionName)
print(inputObject)
end
-- 因為此功能不會返回任何東西,因此此處理器將
-- 「 sink」輸入,並且在此之後不會呼叫其他動作處理器
-- 這個。
end
local navGamepads = UserInputService:GetNavigationGamepads()
for _, gamepad in pairs(navGamepads) do
local supportedKeyCodes = UserInputService:GetSupportedGamepadKeyCodes(gamepad)
for _, keycode in pairs(supportedKeyCodes) do
if keycode == Enum.KeyCode.ButtonX then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.ButtonX)
end
if keycode == Enum.KeyCode.X then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.X)
end
end
end
Position
範例程式碼
處理輸入開始
-- 為了使用輸入開始事件,必須使用 UserInputService 服務
local UserInputService = game:GetService("UserInputService")
-- 提供多種用途案例的樣本功能,用於各種類型的使用者輸入
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)
UserInputState
範例程式碼
綁定支持遊戲控制器鍵碼
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local function actionHandler(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Action Handler: " .. actionName)
print(inputObject)
end
-- 因為此功能不會返回任何東西,因此此處理器將
-- 「 sink」輸入,並且在此之後不會呼叫其他動作處理器
-- 這個。
end
local navGamepads = UserInputService:GetNavigationGamepads()
for _, gamepad in pairs(navGamepads) do
local supportedKeyCodes = UserInputService:GetSupportedGamepadKeyCodes(gamepad)
for _, keycode in pairs(supportedKeyCodes) do
if keycode == Enum.KeyCode.ButtonX then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.ButtonX)
end
if keycode == Enum.KeyCode.X then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.X)
end
end
end
UserInputType
範例程式碼
處理輸入開始
-- 為了使用輸入開始事件,必須使用 UserInputService 服務
local UserInputService = game:GetService("UserInputService")
-- 提供多種用途案例的樣本功能,用於各種類型的使用者輸入
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:", input.KeyCode)
end
if gameProcessed then
print("The game engine internally observed this input!")
else
print("The game engine did not internally observe this input!")
end
end)