# Your First App

In the next 15 minutes you'll build **Can Knockdown**, a tiny physics game where you roll a ball into a pyramid of cans. Then you'll publish it to the web with a link you can send to anyone. You don't need to install anything, and you'll write about 20 lines of code.

This is the finished game. Click it, then use the arrow keys to roll the ball:

[Interactive demo: Can Knockdown, the game you build in this guide](https://playcanv.as/p/gLOk3TQH/)

Along the way you'll use the core ideas that every PlayCanvas app is built from:

- **Entities and components** to make objects and give them a shape, physics or behavior.
- **Materials** to color them.
- **Physics** so they fall, stack and scatter.
- **A script** to read the keyboard.
- **Live Link** to tweak the running game from the Editor.
- **Publishing** to put the game online.

Each step has a short video of exactly what to do. The videos play as you scroll to them, so keep the Editor open in another tab and follow along.

:::info[Before you start]

You need a free [PlayCanvas account](https://login.playcanvas.com/signup) and a desktop browser. The game is played with the arrow keys, so a keyboard helps too.

:::

## 1. Create a Project

Every app lives in a project. Start a new one:

1. Go to your [Projects dashboard](https://playcanvas.com/editor) and select **New project**.
2. Pick the **Blank Project** starter kit.
3. Name it `My First App` and select **Create project**.
4. When it's ready, select **Open project**. The Editor opens in a new tab.

[Video: Creating a project from the Blank Project starter kit on the Projects dashboard](https://developer.playcanvas.com/video/editor-first-app-create-project.mp4)

## 2. Find Your Way Around

The Blank Project starts you with a small scene: a **Camera**, a **Light**, a **Box** and a **Plane** for the floor. Here's where everything lives:

[Image: The PlayCanvas Editor with its five main areas numbered]

1. **[Hierarchy](https://developer.playcanvas.com/user-manual/editor/interface/hierarchy.md)**: every entity in the scene, as a tree. Click one to select it.
2. **[Viewport](https://developer.playcanvas.com/user-manual/editor/interface/viewport.md)**: your 3D view of the scene.
3. **[Inspector](https://developer.playcanvas.com/user-manual/editor/interface/inspector.md)**: the properties of whatever is selected. This is where you'll do most of your editing.
4. **[Assets](https://developer.playcanvas.com/user-manual/editor/assets/asset-panel.md)**: the files your project uses, such as materials, scripts, textures and models.
5. **[Toolbar](https://developer.playcanvas.com/user-manual/editor/interface/toolbar.md)**: tools for moving, rotating and scaling, plus publishing.

Now take the Viewport for a spin. Drag with the left mouse button to orbit, and scroll to zoom. Select **Box** in the Hierarchy, move the mouse back over the Viewport and press <kbd>F</kbd> to focus on it. That's your way home whenever you get lost.

[Video: Orbiting and zooming the Viewport, then pressing F to focus on the Box](https://developer.playcanvas.com/video/editor-first-app-navigate.mp4)

:::tip

<kbd>Shift</kbd>-drag to pan. The [Controls and Keyboard Shortcuts](https://developer.playcanvas.com/user-manual/editor/interface/keyboard-shortcuts.md) page lists everything else.

:::

## 3. Make a Can

An **entity** is an empty object. **Components** give it everything else. The Box is an entity with a **Render** component, which draws a shape. Turn it into a can:

1. Select **Box** in the Hierarchy.
2. In the Inspector, change **Name** to `Can`.
3. In the **RENDER** component, change **Type** from **Box** to **Cylinder**.

It's a can, but a plain white one. Colors and shine come from a **material**:

1. In the Assets panel, select **+** and choose **Material**.
2. Select the new material in the Assets panel and rename it `Can` in the Inspector.
3. Open the **DIFFUSE** section, click the **Color** swatch and enter `E52521`.
4. Open the **SPECULAR** section and set **Metalness** to `0.6` and **Glossiness** to `80`.
5. Drag the material from the Assets panel onto the can in the Viewport.

[Video: Changing the Box into a red metallic Can with a new material](https://developer.playcanvas.com/video/editor-first-app-can.mp4)

## 4. Add Physics

Right now the can is just a picture of a can. Two physics components make it a real object that falls, collides and gets knocked over:

- A **Rigid Body** gives it mass and lets the physics engine move it.
- A **Collision** component gives it a shape for the physics engine to use. This is separate from the visible shape.

Select the **Can** and add both:

1. Select **ADD COMPONENT** and choose **Physics** > **Rigid Body**. Set its **Type** to **Dynamic**, so the can can move.
2. Select **ADD COMPONENT** again and choose **Physics** > **Collision**. Set its **Type** to **Cylinder** and its **Height** to `1`, so it matches the can.
3. A red **Ammo module not found** notice appears. Select **IMPORT AMMO** to add the physics engine to your project.

Without physics on the floor, the can would fall straight through it. Select the **Plane** and give it the same two components:

1. Add a **Rigid Body** and leave its **Type** as **Static**. A static body never moves, but other bodies collide with it.
2. Add a **Collision** component and leave it as a **Box**. Set **Half Extents** to `4`, `0.5`, `4` and **Position Offset** to `0`, `-0.5`, `0`.

[Video: Adding Rigid Body and Collision components to the Can and the Plane, and importing Ammo](https://developer.playcanvas.com/video/editor-first-app-physics.mp4)

:::note[Why those numbers?]

[Collision shapes ignore the entity's **Scale**](https://developer.playcanvas.com/user-manual/physics/collision-shapes.md#scale). The Plane is scaled to 8 × 8, so a box that reaches 4 out from the center in each direction covers it. The offset moves that box down by half its height, so its top sits level with the floor.

:::

## 5. Drop Test

Time to see physics in action. Select the **Can** and drag the green arrow up to lift it into the air. Then select **Launch** (the ▶ button at the top right of the Viewport). Your scene opens in a new tab and runs, and the can drops onto the floor.

[Video: Lifting the Can above the floor and launching the scene to watch it fall](https://developer.playcanvas.com/video/editor-first-app-drop-test.mp4)

Switch back to the Editor tab and press <kbd>Ctrl</kbd>+<kbd>Z</kbd> (<kbd>Cmd</kbd>+<kbd>Z</kbd> on macOS) to put the can back on the floor. Everything you do in the Editor can be undone.

## 6. Stack the Pyramid

One can isn't much of a target. A duplicate copies an entity with all its components, so every copy is already red and ready for physics:

1. With the **Can** selected, select the **Duplicate** button at the top of the Hierarchy, or press <kbd>Ctrl</kbd>+<kbd>D</kbd> (<kbd>Cmd</kbd>+<kbd>D</kbd> on macOS).
2. Set the new can's **Position** from the table below.
3. Repeat until you have all six cans.

| Can | Position X | Position Y |
| --- | --- | --- |
| Bottom row | `-1`, `0`, `1` | `0.5` |
| Middle row | `-0.5`, `0.5` | `1.5` |
| Top | `0` | `2.5` |

Position Z stays `0` for every can.

[Video: Duplicating the Can five times and positioning the copies into a pyramid](https://developer.playcanvas.com/video/editor-first-app-pyramid.mp4)

## 7. Make the Ball

The ball is just another can, reshaped. Duplicate it so it keeps the physics components:

1. With a can selected, duplicate it and rename the copy `Ball`.
2. Set its **Position** to `0`, `0.5`, `3`, which puts it in front of the pyramid.
3. In **RENDER**, set **Type** to **Sphere**. In **COLLISION**, set **Type** to **Sphere**.
4. Create another material and name it `Ball`. Set its **DIFFUSE** **Color** to `1F8FFF` and its **SPECULAR** **Glossiness** to `80`, then drag it onto the ball.

[Video: Duplicating a can into a blue Ball with a sphere shape and its own material](https://developer.playcanvas.com/video/editor-first-app-ball.mp4)

## 8. Frame the Shot

The **Camera** decides what the player sees. Select it in the Hierarchy and a live preview of its view appears in the corner of the Viewport. Point it down the length of the floor:

1. Set **Position** to `0`, `4`, `9`.
2. Set **Rotation** to `-20`, `0`, `0`.

[Video: Positioning the Camera and watching its preview update](https://developer.playcanvas.com/video/editor-first-app-camera.mp4)

## 9. Script the Controls

Components do a lot, but behavior that's specific to your game comes from **scripts**. This one pushes the ball when you press the arrow keys.

1. Select the **Ball** and choose **ADD COMPONENT** > **Script**.
2. In the **+ ADD SCRIPT** field, type `roll.mjs` and press <kbd>Enter</kbd>. The `.mjs` extension creates an [ESM script](https://developer.playcanvas.com/user-manual/scripting/esm-scripts.md), the modern kind of script built on JavaScript modules (ECMAScript modules, or ESM). The script is attached to the ball and appears in the Assets panel.
3. Select the **Edit** (pencil) button on the **roll** script. The Code Editor opens in a new tab.
4. Replace everything in the file with the code below, then press <kbd>Ctrl</kbd>+<kbd>S</kbd> (<kbd>Cmd</kbd>+<kbd>S</kbd> on macOS) to save.
5. Back in the Editor, select the **Parse** button (the two arrows next to **Edit**). A **power** field appears under the script.

```javascript
import { Script, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN } from 'playcanvas';

export class Roll extends Script {
    static scriptName = 'roll';

    /**
     * How hard the arrow keys push the ball.
     *
     * @attribute
     */
    power = 15;

    update(dt) {
        const keyboard = this.app.keyboard;

        // Which way are the arrow keys pointing?
        let x = 0;
        let z = 0;
        if (keyboard.isPressed(KEY_LEFT)) x -= 1;
        if (keyboard.isPressed(KEY_RIGHT)) x += 1;
        if (keyboard.isPressed(KEY_UP)) z -= 1;
        if (keyboard.isPressed(KEY_DOWN)) z += 1;

        // Push the ball while a key is held. Physics does the rest.
        this.entity.rigidbody.applyForce(x * this.power, 0, z * this.power);
    }
}
```

[Video: Adding the roll script to the Ball, pasting the code in the Code Editor and parsing it](https://developer.playcanvas.com/video/editor-first-app-script.mp4)

Here's what the script does:

- `update` runs once every frame, about 60 times a second.
- `keyboard.isPressed` checks whether a key is held down right now. Up moves away from the camera, which is the negative Z direction.
- `applyForce` pushes the ball's rigid body. You never move the ball yourself: the physics engine works out the rolling, the bouncing and every can it hits.
- The `@attribute` comment turns `power` into an editable field in the Inspector. That's why you parsed the script, and it means you can tune the game without touching the code.

## 10. Play

Select **Launch** again. Hold the up arrow to roll the ball, steer with left and right, and aim between two cans for the biggest crash.

[Video: Playing the game: the ball rolls into the pyramid and the cans scatter](https://developer.playcanvas.com/video/editor-first-app-play.mp4)

To play again, reload the tab.

:::tip[Not working?]

- **Nothing falls:** check that you selected **IMPORT AMMO**, and that the cans' Rigid Body **Type** is **Dynamic**.
- **The ball won't move:** click inside the game first so it has keyboard focus. Then check that the **power** field appears under the roll script. If it doesn't, select **Parse** again.
- **The cans fall through the floor:** check the Plane's Collision **Half Extents** and **Position Offset**.

:::

## 11. Tweak It Live

The Launch tab stays connected to the Editor through **Live Link**. Change something in the Editor and the running game updates at once, with no reload. Put the two windows side by side and try it:

- Select the **Settings** (gear) button at the top of the Viewport, open **PHYSICS** and set the **Gravity** Y value to `-1.5`. Now roll the ball: it's playing on the Moon.
- Select the **Ball** material and give it a new **Color**.
- Select the **Ball** and turn **power** up to `40`.

[Video: The Editor and the running game side by side: lowering gravity and recoloring the ball update the game instantly](https://developer.playcanvas.com/video/editor-first-app-live-link.mp4)

When you're done, set **Gravity** back to `-9.8`, or keep it. It's your game.

Live Link covers scene and asset changes. After you edit the code in a script, reload the Launch tab to run the new version.

## 12. Publish

Last step: put your game on the web.

1. Select the **Publish / Download** button in the toolbar on the left.
2. Select **Publish**.
3. Your build gets your project's name as its title. Select **Publish** at the bottom of the form.

[Video: Publishing a build and getting its playcanv.as link](https://developer.playcanvas.com/video/editor-first-app-publish.mp4)

A few seconds later your game is live, with a link like `https://playcanv.as/p/...`. The game at the top of this page was published exactly this way. Open it on your phone, send it to a friend, post it anywhere.

Your first build automatically becomes the project's **primary build**, which is what that link plays. When you publish again, you choose when the new build replaces it, so you can test a build before your players see it. See [PlayCanvas Hosting](https://developer.playcanvas.com/user-manual/editor/publishing/playcanvas-hosting.md) to manage builds, or [Downloading a Build](https://developer.playcanvas.com/user-manual/editor/publishing/downloading.md) to download a build and host it yourself.

## You Made a Game

In one sitting you've used the same building blocks as every PlayCanvas app: entities and components, materials, rigid bodies and collision, a script with an editable attribute, Live Link and publishing. Everything else builds on them.

### Make It Yours

Some ideas for what to try next, roughly from easiest to hardest:

- **Build a harder level.** Stack a taller pyramid, or add walls with more static Box entities.
- **Change the look.** Try the other material sections, add more lights, or drop in a model from the [Asset Store](https://developer.playcanvas.com/user-manual/editor/assets/asset-store.md).
- **Bring your own art.** [Import](https://developer.playcanvas.com/user-manual/editor/assets/importing.md) your own models, textures and sounds.
- **Keep score.** Use [collision events](https://developer.playcanvas.com/user-manual/physics/collision-events.md) to count hits, and show the score with a [text element](https://developer.playcanvas.com/user-manual/user-interface.md).
- **Add a reset key.** Store each can's starting position in a script and put it back when <kbd>R</kbd> is pressed.

### Keep Learning

- [Scripting](https://developer.playcanvas.com/user-manual/scripting.md): everything scripts can do.
- [Physics](https://developer.playcanvas.com/user-manual/physics.md): forces, joints, triggers and ray casts.
- [The Editor interface](https://developer.playcanvas.com/user-manual/editor/interface.md): every panel in depth.
- [Tutorials](https://developer.playcanvas.com/tutorials/): complete projects to explore and fork, and step-by-step series such as [Making a Simple Game](https://developer.playcanvas.com/tutorials/keepyup-part-one/), which builds the Keepy Up game.
