A first person character controller for the Three.js graphics library
npm install charactercontroller
import CharacterController from "charactercontroller";
// Scene & renderer initialisation...
const controller = new CharacterController(scene, options);
function animate() {
requestAnimationFrame(animate);
// ...
controller.update();
renderer.render(scene, controller.camera);
}-
sceneAn instance of
THREE.Scenethat the Character Controller is to become a child of. -
optionsAn object defining options for the Character Controller. The valid fields are described below
-
walkSpeedThe rate at which the controller is translated in the scene in response to player inputs, when the sprint key (left shift) is not being pressed.
- Default:
5
- Default:
-
sprintSpeedThe rate at which the controller is translated in the scene in response to player inputs, when the sprint key (left shift) is being pressed.
- Default:
10
- Default:
-
floorDistanceHow far above a surface the controller can get before stopping falling.
Could be interpreted as the height of the player.
- Default:
1
- Default:
-
gravityHow quickly the controller is pulled down when there is no surface beneath it.
- Default:
-9.81
- Default:
-
jumpPowerWith how much force the controller is projected upwards when a jump is initiated.
- Default:
5
- Default:
-
sensitivity-
xHow much the camera should move in response to the player moving the mouse left and right.
- Default:
0.1
- Default:
-
yHow much the camera should move in response to the player moving the mouse up and down.
- Default:
0.1
- Default:
-
-
lookLimit-
downThe angle in degrees that the camera's
xrotation should be clamped to when looking down- Default:
0
- Default:
-
upThe angle in degrees that the camera's
xrotation should be clamped to when looking up- Default:
180
- Default:
-
-
cameraFovThe field of view of the camera attatched to the character controller.
- Default:
75
- Default:
-
inputMappingsThe
KeyboardEvent.codes that control the character controller. An array ofcodes are used to support multiple keys controlling the same actions; primarily for accessability reasons.-
scalarNote
The scalar property defines axes that can take on any value within a range. There are two axes that control the planar movement of the character controller; horizontal and vertical, named
horizontalAxisandverticalAxisrespectively. These both take an array of input maps as values. The format of the input maps required in arrays on scalar axes is as follows;{ inputs: KeyboardEvent.code[], value: number }Where
valueis the magnitude of the axis when a key ininputsis being pressed.horizontalAxis- Default:
{ inputs: ["KeyA", "ArrowLeft"], value: -1 }, { inputs: ["KeyD", "ArrowRight"], value: 1 },
- Default:
verticalAxis- Default:
{ inputs: ["KeyS", "ArrowDown"], value: -1 }, { inputs: ["KeyW", "ArrowUp"], value: 1 },
- Default:
-
discreteNote
The discrete property defines single-fire actions that occur at a specific point in time. These inputs differ from the ones defined under
scalaras they describe events that happen once at a time, rather than continuously over time. The format of input maps required here is simply an array ofKeyboardEvent.codes.jump- Default:
["Space"]
- Default:
sprint- Default:
["ShiftLeft", "ShiftRight"],
- Default:
Note
The default
inputMappingsobject is shown below;inputMappings = { scalar: { horizontalAxis: [ { inputs: ["KeyA", "ArrowLeft"], value: -1 }, { inputs: ["KeyD", "ArrowRight"], value: 1 }, ], verticalAxis: [ { inputs: ["KeyS", "ArrowDown"], value: -1 }, { inputs: ["KeyW", "ArrowUp"], value: 1 }, ], }, discrete: { jump: ["Space"], sprint: ["ShiftLeft", "ShiftRight"], }, },
-