Block Entities

Block entities are exactly what they sound like. A block, that behaves like an entity. This allows you to create blocks of any size that interact with the physical world. They can move, rotate, have impulses and velocities applied to them, and so much more.

Common Uses & Examples

Let's dive into some of the most common use cases of block entities.

Moving Platform

Probably the most common use, let's create a block entity that is a platform in the sky that moves at a constant speed in the +z/-z axis direction.

const blockPlatform = new Entity({
  blockTextureUri: 'blocks/grass', // A texture URI without a file extension will use a folder and look for the textures for each face in the folder (-x.png, +x.png, -y.png, +y.png, -z.png, +z.png)
  blockHalfExtents: { x: 1, y: 0.5, z: 1 },
  rigidBodyOptions: {
    type: RigidBodyType.KINEMATIC_VELOCITY, // Kinematic means platform won't be effected by external physics, including gravity
    linearVelocity: { x: 0, y: 0, z: 3 }, // A starting velocity that won't change because it's kinematic
  },
});

// Clamp the z range the platform moves back and forth between
blockPlatform.on(EntityEvent.TICK, () => {
  const position = blockPlatform.getTranslation();

  if (position.z < -9) {
    blockPlatform.setLinearVelocity({ x: 0, y: 0, z: 3 });
  }

  if (position.z > 8) {
    blockPlatform.setLinearVelocity({ x: 0, y: 0, z: -3 });
  }
});

blockPlatform.spawn(world, { x: 3, y: 3, z: -7 });

Pushable Block

A block that we can push is great for puzzles and other things.

Pushable Block With Locked Rotations & Translations

Teleporting Block

Perhaps we want to create a block that teleports to different coordinate every few seconds for a platforming game, here's how we can do that.

Heavy Pushable Block

Pushable blocks are fun, but what if we want a block that multiple entities would need to push to be able to easily move? We can do that by creating a block with a heavier mass.

Last updated