Tutorial suggestion on Gamepad

@playcanvas you sould create a tutorial demonstrating how to use gamepad input by controling a player or something with it. Your API Reference explains what it does and how to use it, but for people like me, it’s easier to understand when it is being used in action.

2 Likes

I as well am curious, not just for controllers linked up via USB, but also over Bluetooth, such as a Moga Rebel in conjunction with an iOS or Android device.

1 Like

I totally agree, guys. We need to create a GamePad tutorial. But in the meantime, my Zombie Pac-Man project supports gamepads:

https://playcanvas.com/project/331628/overview/zombie-pacman

Unfortunately, forking is disabled because it contains licensed assets. But the code is here:

https://playcanvas.com/editor/code/331628/player.js

Specifically:

            var padDown = app.gamepads.isPressed(pc.PAD_1, pc.PAD_DOWN);
            var padUp = app.gamepads.isPressed(pc.PAD_1, pc.PAD_UP);
            var padLeft = app.gamepads.isPressed(pc.PAD_1, pc.PAD_LEFT);
            var padRight = app.gamepads.isPressed(pc.PAD_1, pc.PAD_RIGHT);
            var keyDown = app.keyboard.isPressed(pc.KEY_DOWN);
            var keyUp = app.keyboard.isPressed(pc.KEY_UP);
            var keyLeft = app.keyboard.isPressed(pc.KEY_LEFT);
            var keyRight = app.keyboard.isPressed(pc.KEY_RIGHT);

            if (padDown || keyDown) this.desiredDir = 0;
            if (padUp || keyUp) this.desiredDir = 1;
            if (padLeft || keyLeft) this.desiredDir = 2;
            if (padRight || keyRight) this.desiredDir = 3;

So you can either use the DPad or the arrow keys to move the player character. Other API functionality documented here:

But yeah, we need more comprehensive examples for this. :smile: