A class that allows you to switch between first, second and third person perspectives for a RigidControls instance. By default, the key to switch between perspectives is C.
Example
// Initialize the perspective with the rigid controls.
const perspective = new VOXELIZE.Perspective(controls, world);
// Bind the keyboard inputs to switch between perspectives.
perspective.connect(inputs, "in-game");
// Switch to the first person perspective.
perspective.state = "third";
// Update the perspective every frame.
perspective.update();
Constructors
constructor
• new Perspective(controls
, world
, options?
): Perspective
Create a new perspective instance that is attached to the given rigid controls. The default perspective is the first person perspective.
Parameters
Name | Type | Description |
---|---|---|
controls | RigidControls | The rigid controls that this perspective instance is attached to. |
world | World <any > | The world that this perspective instance is working with. |
options | Partial <PerspectiveOptions > | Parameters to configure the perspective. |
Returns
Properties
INPUT_IDENTIFIER
▪ Static
Readonly
INPUT_IDENTIFIER: "voxelize-perspective"
This is the identifier that is used to bind the perspective's keyboard inputs when Perspective.connect is called.
controls
• controls: RigidControls
The rigid controls that this perspective instance is attached to.
inputs
• Optional
inputs: Inputs
<any
>
The input manager that binds the perspective's keyboard inputs.
onChangeState
• onChangeState: (state
: "first"
| "second"
| "third"
) => void
Type declaration
▸ (state
): void
A method that can be implemented and is called when the perspective's state changes.
Parameters
Name | Type |
---|---|
state | "first" | "second" | "third" |
Returns
void
options
• options: PerspectiveOptions
Parameters to configure the perspective.
world
• world: World
<any
>
The world that this perspective instance is working with.
Accessors
state
• get
state(): "first"
| "second"
| "third"
Getter for the perspective's state.
Returns
"first"
| "second"
| "third"
• set
state(state
): void
Setter for the perspective's state. This will call Perspective.onChangeState if it is implemented.
Parameters
Name | Type |
---|---|
state | "first" | "second" | "third" |
Returns
void
Methods
connect
▸ connect(inputs
, namespace?
): () => void
Connect the perspective to the given input manager. This will bind the perspective's keyboard inputs, which by default is C to switch between perspectives. This function returns a function that when called unbinds the perspective's keyboard inputs. Keep in mind that remapping the original inputs will render this function useless.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
inputs | Inputs <any > | undefined | The Inputs instance to bind the perspective's keyboard inputs to. |
namespace | string | "*" | The namespace to bind the perspective's keyboard inputs to. |
Returns
fn
▸ (): void
Returns
void
toggle
▸ toggle(inverse?
): void
Toggle between the first, second and third person perspectives. The order goes from first person to third person and then to second person.
Parameters
Name | Type | Default value |
---|---|---|
inverse | boolean | false |
Returns
void
update
▸ update(): void
This updates the perspective. Internally, if the perspective isn't in first person, it raycasts to find the closest block and then ensures that the camera is not clipping into any blocks.
Returns
void