Model Entities

Model entities are an entity that is visually representing by a .gltf or .glb model. Model entities are the most common type of entity you'll use. For example, players, enemies in your game, fixed and moving elements like lamp-posts or birds, and more, will typically be created using an Entity.

Common Uses & Examples

Let's dive into some common patterns when using model entities.

Bullet Entity

Some games may want a bullet that moves through the physical world and registers a hit when it intersects with another entity. You could alternatively just use a raycast for this, but in this example we'll use an entity.

const bullet = new Entity({
  name: 'Bullet',
  modelUri: 'models/bullet.gltf',
  modelScale: 0.3,
  rigidBodyOptions: {
    type: RigidBodyType.KINEMATIC_VELOCITY, // Kinematic means entity's rigid body will not be affected by physics. KINEMATIC_VELOCITY means the entity is moved by setting velocity.
    linearVelocity: { // Setting the linear velocity on a Kinematic type means it will move at a constant speed, unaffect by gravity or other forces
      x: direction.x * BULLET_SPEED,
      y: direction.y * BULLET_SPEED,
      z: direction.z * BULLET_SPEED,
    },
  },
});

bullet.onEntityCollision = (bullet: Entity, otherEntity: Entity, started: boolean) => {
  if (!started) { return } // we only care about the initial hit, not when it stops
  
  // Perform your bullet logic here, the otherEntity is the entity the bullet hit.
  // You can do things like despawn the entity that was hit, use your own damage
  // logic, and more. 
}

Extra Large Entity

With model entities, you can control their physical scaling in the world. This allows you to scale up or down the model of the entity relative to its initial size. We'll use model scale to create a massive spider.

const spider = new Entity({
  modelUri: 'models/npcs/spider.gltf',
  modelScale: 10, // 10x the default scale
  modelLoopedAnimations: [ 'idle' ],
});

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

Now, we'll see a massive spider!

Last updated