Simple Entity Controller

The SimpleEntityController class provides simple movement controls for entities. It's a great starting point if you're building basic pathfinding of have simple movement needs of your entities. You can piece together it's .move()and .face()methods to create simple pathfinding and behaviors relatively quickly.

Using a SimpleEntityController with an Entity

You can assign a SimpleEntityController to your Entity when it spawns or at any point in its lifecycle. In this example, we'll assign the controller when our entity spawns.

import {
  Entity,
  SimpleEntityController,
  // ... other imports
} from 'hytopia';

// other code ...

// Create a spider entity instance with our entity controller
const spider = new Entity({
  controller: new SimpleEntityController(),
  modelUri: 'models/npcs/spider.gltf',
  modelScale: 2.5,
  modelLoopedAnimations: [ 'idle' ],
});

// Spawn the spider in the world.
spider.spawn(world, { x: 0, y: 10, z: -15 });

// have the spider face towards and move towards the target
// position, taking physics into account.
const target = { x: 0, y: 0, z: 0 };
const spiderEntityController = spider.controller as SimpleEntityController;
spiderEntityController.move(target, 3); // move towards target at a speed of 3 (approx. blocks per second)
spiderEntityController.face(target, 1); // face towards the target a speed of 1

SimpleEntityController Implementation

For the sake of demonstration and reference for creating your own controllers or modifying the existing SimpleEntityController, you can find the current internal implementation below:

Diving Deeper

The SimpleEntityController class is constantly evolving. You can find the latest SimpleEntityController API Reference here.

If there are features that we don't currently support for the SimpleEntityController that you'd like to see added to the HYTOPIA SDK, you can submit a feature request here.

Last updated