Input and camera
Keyboard and mouse events come through Input (InputBegan, InputChanged, InputEnded and friends; see Input). This page covers the camera, gamepads, touch and named actions.
Camera
Scene.CurrentCamera is what gets drawn. It stays where scripts put it until you give it a CameraSubject (a part or model). Then it follows the subject each step according to CameraType:
| Type | Behaviour |
|---|---|
Custom (default), Orbital | Orbit the subject. Right-drag turns, the wheel zooms between MinZoomDistance (0.5) and MaxZoomDistance (400). Zoom is the current distance |
Follow | Orbit, and swing round behind the subject as it moves |
Attach | Stay behind the subject, turning with it |
Watch | Stay put and look at the subject |
Track | Keep the same offset, without turning |
Fixed, Scriptable | Leave it to your scripts |
local camera = Scene.CurrentCamera
camera.CameraSubject = hero
camera.CameraType = Enum.CameraType.Follow
camera.MaxZoomDistance = 30The camera looks at the subject's pivot plus FocusOffset (1.5 studs up, about head height). An orbiting camera with a wall in the way moves in front of the wall, ignoring the subject's own model, and backs out once it's clear. The first gamepad's right stick turns it like the mouse. None of this is saved in a place.
Gamepads
Up to eight gamepads, as Enum.UserInputType.Gamepad1 to Gamepad8, through SDL, which knows most controllers.
- Buttons fire
InputBegan/InputEndedwith the gamepad asUserInputTypeand the button asKeyCode:ButtonA,ButtonB,ButtonX,ButtonY(named by position, Xbox style),ButtonL1,ButtonR1,ButtonL3,ButtonR3,ButtonStart,ButtonSelectand theDPaddirections. - Sticks (
Thumbstick1,Thumbstick2) fireInputChangedwithPositionfrom -1 to 1, Y up. - Triggers (
ButtonL2,ButtonR2) fireInputChangedwithPosition.Zfrom 0 to 1.
| Member | Is |
|---|---|
Input:GetConnectedGamepads(), GetGamepadConnected(pad) | Which are plugged in |
GamepadConnected, GamepadDisconnected | Events |
GamepadEnabled | True while any is connected |
IsGamepadButtonDown(pad, keyCode) | A trigger counts once pulled past half |
GetGamepadState(pad) | The latest InputObject per button and stick |
Touch
Each finger fires InputBegan, InputChanged and InputEnded with Enum.UserInputType.Touch, plus TouchStarted, TouchMoved and TouchEnded with an InputObject that stays the same from touch to lift. A touch that lifts within 0.3 s and 10 pixels also fires TouchTap with its position. TouchEnabled turns true at the first touch. Pinch, pan and rotate gestures aren't built.
Actions
Actions binds keys and buttons to named actions, so different parts of a game can share the keyboard without fighting.
Actions:BindAction("Jump", function(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
jump()
end
return Enum.ContextActionResult.Sink
end, false, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)BindAction(name, fn, createTouchButton, ...inputs)takesKeyCodes or wholeUserInputTypes (MouseButton1,Keyboard,Gamepad1). Binding a name again replaces it.createTouchButtondoes nothing yet.BindActionAtPriority(name, fn, createTouchButton, priority, ...inputs). Priority is a number orEnum.ContextActionPriority:Low1000,Default/Medium2000,High3000.BindActionuses 2000.- Input goes to the highest priority first, and the most recently bound on a tie. Return
Passto hand it on. Returning nothing, erroring or yielding sinks it. - The release goes to whichever action sank the press. If that action is unbound, or another is bound to the same input while it's held, it gets
Cancel. - Input the UI used never reaches actions. Sunk input still fires
Input.InputBeganand so on, withgameProcessedEventtrue. UnbindAction(name),UnbindAllActions(),GetBoundActionInfo(name),GetAllBoundActionInfo(). Actions unbind when the script that bound them stops.
Fake input
VirtualInput sends made-up keyboard, mouse, gamepad and touch input for tests. See Testing.