Skip to main content

Scripting

Scripts are the heart of interactivity in PlayCanvas. They're reusable pieces of code that you attach to Entities to define behaviors, handle user input, manage game logic, and bring your projects to life.

Using the Editor?

If you're using the PlayCanvas Editor, check out the Editor Scripting section to learn about managing scripts, the code editor, VS Code integration, and hot reloading.

Two Scripting Systems

PlayCanvas supports two scripting approaches:

  • ESM Scripts (.mjs files) — Modern ES Module-based scripts using class syntax. Recommended for new projects.
  • Classic Scripts (.js files) — The original PlayCanvas scripting system using prototype-based syntax.

Both systems can coexist in the same project, allowing you to migrate gradually or use whichever approach fits your needs.

Quick Example

Here's a simple script that rotates an entity:

import { Script } from 'playcanvas';

export class Rotate extends Script {
static scriptName = 'rotate';

/** @attribute */
speed = 10;

update(dt) {
this.entity.rotate(0, this.speed * dt, 0);
}
}

In This Section