Player Controlled Entities

Player controlled entities, represented by the PlayerEntity class, are entities that are controlled by a player's inputs. Player controlled entities can be either a Model Entitiesor a Block Entities. Yes, that means if you really want, you could even have players control a movable block in your game!

PlayerEntity Class & Entity Controllers

The behavior of the PlayerEntity class based on player inputs is dependent on the logic in it's entity controller (playerEntityInstance.controller).

In our Entity Controllers section, we covered the lifecycle of an entity controller. Entity controllers have a special lifecycle method called tickWithPlayerInput(). A PlayerEntity invokes this method of it's controller every tick, passing the current inputs of the player to it.

Through this, the PlayerEntity class, which represents a player controlled entity, is controlled through the logic of it's entity controller based on the input of the player each tick.

To learn more about player inputs and controls, check out Input & Controls

Creating A PlayerEntity

Here's an example from our boilerplate, showcasing how we create a new player entity controlled by a player when they join our game.

world.onPlayerJoin = player => {
  const playerEntity = new PlayerEntity({
    // PlayerEntity accepts an additional property 
    // in its options, player, which is the player
    // who's inputs will control the actions of this
    // entity. The default PlayerEntityController()
    // is assigned to this entity, since we did not
    // override it by specifying the `controller: new MyCustomController()`
    // property option.
    player,
    name: 'Player',
    modelUri: 'models/players/player.gltf',
    modelLoopedAnimations: [ 'idle' ],
    modelScale: 0.5,
  });
  
  playerEntity.spawn(world, { x: 0, y: 10, z: 0 });
};

You can learn more about the PlayerEntity class options here.

Last updated