LogoLogo
  • Get Started
  • Getting Started
    • Initial Setup
    • Create Your First Game
    • API Reference
    • Build Your First World Map
    • Multiplayer Testing
    • Use Templates & Examples
    • Styling & Assets
      • Modeling Guidelines
      • Texturing Guidelines
      • Default Assets
  • Build Faster With AI Tools
  • SDK Guides
    • Assets
    • Audio & SFX
      • Audio Manager
    • Blocks & Chunks
      • Block Types
      • Block Type Registry
      • Chunks
      • Chunk Lattice
    • Camera
    • Chat & Commands
    • Debugging
    • Entities
      • Animations
      • Block Entities
      • Colliders & Hitbox
      • Child Entities
      • Entity Controllers
        • Base Entity Controller
        • Pathfinding Entity Controller
        • DefaultPlayer Entity Controller
        • Simple Entity Controller
      • Entity Manager
      • Model Entities
      • Movement & Pathfinding
      • Player Controlled Entities
    • Events
    • Input & Controls
    • Lighting
      • Ambient Light
      • Light Manager
      • Point Lights
      • Spot Lights
      • Sun Light (Directional)
    • Mobile
    • Persisted Data
    • Players
      • Player Manager
      • Persisted Player Data
    • Plugins
    • Physics
      • Colliders
      • Collision Groups
      • Debugging
      • Gravity
      • Raycasts
      • Rigid Bodies
    • User Interface
      • Overlay UI
      • Scene UIs
      • Scene UI Manager
    • Worlds
      • Map Data Format
  • Helpful Resources
    • HYTOPIA Architecture & Platform Overview
    • Useful Third-Party Tools
Powered by GitBook
On this page
  • DefaultPlayerEntity Class
  • PlayerEntity Class & Entity Controllers
  • Creating A PlayerEntity
Export as PDF
  1. SDK Guides
  2. Entities

Player Controlled Entities

PreviousMovement & PathfindingNextEvents

Last updated 9 days ago

Player controlled entities, represented by the , 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!

DefaultPlayerEntity Class

If you only need the default player character that can move around, and supports all standard behaviors for HYTOPIA, you'll use an instance of the DefaultPlayerEntity class.

The PlayerEntity class is primarily meant for situations where you need to have full control over player behaviors for inputs, their representative model, etc. PlayerEntity is a lower level implementation that gives more control compared to DefaultPlayerEntity.

PlayerEntity Class & Entity Controllers

The behavior of the PlayerEntity class based on player inputs is dependent on the logic in it's entity controller (playerEntity.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.on(PlayerEvent.JOINED_WORLD, ({ 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 .

PlayerEntity class
learn more about the PlayerEntity class options here