Skip to content

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:

TypeBehaviour
Custom (default), OrbitalOrbit the subject. Right-drag turns, the wheel zooms between MinZoomDistance (0.5) and MaxZoomDistance (400). Zoom is the current distance
FollowOrbit, and swing round behind the subject as it moves
AttachStay behind the subject, turning with it
WatchStay put and look at the subject
TrackKeep the same offset, without turning
Fixed, ScriptableLeave it to your scripts
luau
local camera = Scene.CurrentCamera
camera.CameraSubject = hero
camera.CameraType = Enum.CameraType.Follow
camera.MaxZoomDistance = 30

The 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/InputEnded with the gamepad as UserInputType and the button as KeyCode: ButtonA, ButtonB, ButtonX, ButtonY (named by position, Xbox style), ButtonL1, ButtonR1, ButtonL3, ButtonR3, ButtonStart, ButtonSelect and the DPad directions.
  • Sticks (Thumbstick1, Thumbstick2) fire InputChanged with Position from -1 to 1, Y up.
  • Triggers (ButtonL2, ButtonR2) fire InputChanged with Position.Z from 0 to 1.
MemberIs
Input:GetConnectedGamepads(), GetGamepadConnected(pad)Which are plugged in
GamepadConnected, GamepadDisconnectedEvents
GamepadEnabledTrue 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.

luau
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) takes KeyCodes or whole UserInputTypes (MouseButton1, Keyboard, Gamepad1). Binding a name again replaces it. createTouchButton does nothing yet.
  • BindActionAtPriority(name, fn, createTouchButton, priority, ...inputs). Priority is a number or Enum.ContextActionPriority: Low 1000, Default/Medium 2000, High 3000. BindAction uses 2000.
  • Input goes to the highest priority first, and the most recently bound on a tie. Return Pass to 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.InputBegan and so on, with gameProcessedEvent true.
  • 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.

Apache License 2.0