PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Movement and Camera Controls

Movement and Camera Controls

Jul 03 2018, 10:53 AM PST 5 min

The controls for the player’s camera and character are defined in LocalScripts. By default these scripts are copied into a player’s PlayerScripts when the player joins the game.

Dev setting vs User setting

Roblox supports several different control schemes, both for the camera and for player movement. By default, which control setup a player uses is based on the setting they configure in the game menu. However, if a developer wants to enforce a certain mode in their game they can set the modes they want players to user in the Properties of StarterPlayer.

All of the override properties are prefixed with the word “dev” (e.g. DevCameraOcclusionMode). All of these properties are set by default to “UserChoice”. If left at this setting, then the control and camera scripts will use whatever the user’s settings are. If these properties are changed, then the control and camera scripts will use whatever is set in the property to determine which mode the players will use.

Default Control scripts

To see the default control scripts, launch studio and press the Play button in the Test tab. Roblox automatically inserts the scripts inside the PlayerScipts Instance in the Player. This contains the ControlScript which has several parts:

Action Description
ControlScript Sets up controls based on user settings.
MasterControl Interface to send controls from active control script to the player character.
DPad Handles Dpad input on touch devices.
Gamepad Handles input from gamepad controllers.
KeyboardMovement Handles input from keyboard.
Thumbpad Handles input from touch devices using virtual thumbpad.
Thumbstick Handles input from touch devices using virtual thumbstick.
TouchJump Handles input from jump button on touch devices using thumbstick and thumbpad.
VehicleController Handles input for VehicleSeats

Default Camera scripts

To see the default camera scripts, launch studio and press the Play button in the Test tab. Roblox automatically inserts the scripts inside the PlayerScipts Instance in the Player. This contains the CameraScript which has several parts:

Action Description
CameraScript Manages the Player's camera and checks for any camera setting changes.
ClickToMove Handles camera and movement for the ClickToMove movement mode. Ensures the camera is in line with the player character and the destination.
Invisicam Handles the Invisicam occlusion mode. Will make parts locally transparent if they are in-between the player character and the camera.
Poppercam Handles the Zoom occlusion mode. Will zoom the camera if parts are in-between the player character and the camera.
RootCamera Handles basic camera manipulation and listens for input from the player.
ClassicCamera Handles the Classic CameraMode.
FollowCamera Handles the Follow CameraMode.
ShiftLockController Handles shift toggle of mouselock.

Custom control scripts

You can overwrite these control scripts by using StarterPlayer > PlayerScripts. All scripts inside of PlayerScripts get copied to a player when that player joins the game. If you name these scripts the same as one of the default control scripts (CameraScript and ControlScript respectively), your script will overwrite the default.

Example

This example implements a simple side-scrolling camera and movement scheme. The camera stays locked following the player’s character side to side, and the player can only move left and right. The camera is controlled by a LocalScript called CameraScript, and character movement is controlled by a LocalScript called ControlScript. Both of these scripts are placed in StarterPlayerScripts so they overwrite the default scripts:

PlayerScripts.png

CameraScript

-- Declare variables
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local runService = game:GetService("RunService")

-- Wait a frame for camera to load
wait()

-- Update camera on RenderStepped to get smooth motion
runService.RenderStepped:connect(function()
  local character = player.Character
  -- Check if character and torso exist (in case character is dead)
  if character and character.Torso then
    local torso = character.Torso
    
    -- Set cframe of the camera so the camera's X position matches
    -- the character's
    local newCameraPosition = Vector3.new(torso.Position.X, 5, 20)
    camera.CoordinateFrame = CFrame.new(newCameraPosition)
  end
end)

ControlScript

-- Declare varaibles
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local moveVector = Vector3.new(0,0,0)

-- Handle inputBegan event (when player first presses a key)
userInputService.InputBegan:connect(function(inputObject)
  if player.Character then
    if inputObject.KeyCode == Enum.KeyCode.A then
      moveVector = moveVector + Vector3.new(-1,0,0)
    end
    if inputObject.KeyCode == Enum.KeyCode.D then
      moveVector = moveVector + Vector3.new(1,0,0)
    end
    if inputObject.KeyCode == Enum.KeyCode.Space then
      player.Character.Humanoid.Jump = true
    end
  end
end)

-- Handle inputEnded event (when player releases a key)
userInputService.InputEnded:connect(function(inputObject)
  if player.Character then
    if inputObject.KeyCode == Enum.KeyCode.A then
      moveVector = moveVector + Vector3.new(1,0,0)
    end
    if inputObject.KeyCode == Enum.KeyCode.D then
      moveVector = moveVector + Vector3.new(-1,0,0)
    end
  end
end)

-- On renderstepped update the character's motion based on what keys
-- are currently being pressed
runService.RenderStepped:connect(function()
  if player.Character then
    player.Character.Humanoid:Move(moveVector)
  end
end)
Tags:
  • camera
  • input
  • controls
  • reference