Gamepad help please

Yea I’ll test this! I’m sure it will work too looks like it.

So perhaps we should make this remark to the Playcanvas folk on fixing it. Because that seems like a lot of extra code to make an already functional code for other keys.

I have a workaround actually for this, but was curious to why it was so difficult to implement. Thanks :slight_smile:

1 Like

Guys, if you think you’ve found a bug in the engine, feel free to log here. Thanks! :smile:

Have done it :smiley:

BACK AGAIN! Thought I reignite this thread :slight_smile:

So it seems that just basic functionality for gamepads that I use to use for older versions of PlayCanvas don’t work any more. Or maybe I’m missing something

if(this.app.gamepads.isPressed(pc.PAD_1 , pc.PAD_UP)){
        console.log('pressed up');
}

I get an error that takes me to ‘isPressed’ and says it’s null :S not sure what’s going on but thought I’d let you all know. Would be great to get this to work for my game project!

Also is there a way to let playcanvas know if there are gamepads at all?

Do you see the gamepad in this tester? http://html5gamepad.com/

Hey - you need to enable Gamepads under the Input Settings:

45%20PM

2 Likes

Oh what!!! Is that new?

I was about to do it via straight Javascript lol. Thanks!

how do you code down and left on the sticks

@Leonidas

@WilliamBoersma31

Quick example:

var Main = pc.createScript('main');

// initialize code called once per entity
Main.prototype.initialize = function() {
    this.app.gamepads.deadZone = 0.1;
};

// update code called every frame
Main.prototype.update = function(dt) {
    var gamepads = this.app.gamepads;
    var x = gamepads.getAxis(pc.PAD_1, pc.PAD_L_STICK_X);
    var y = gamepads.getAxis(pc.PAD_1, pc.PAD_L_STICK_Y);
    if (x !== false && y !== false) {
        console.log("x: " + x.toPrecision(3) + " | y: " + y.toPrecision(3));
    }
};

Some more examples:
http://playcanvas.github.io/#input/gamepad.html

You can find a bunch of constants for the pad here: https://developer.playcanvas.com/en/api/pc.html#PAD_1

@yaustar thanks I’m gonna go add this to my game

can anyone tell me what is wrong with this line of code?

What’s the error message?

[Controller%20Input.js?id=40793921&branchId=1303e9b3-0978-47b7-b1c7-7f286697e2fb:11]: Cannot read property ‘isPressed’ of null

TypeError: Cannot read property ‘isPressed’ of null
at scriptType.ControllerInput.update (https://launch.playcanvas.com/api/assets/files/Scripts/Controller%20Input.js?id=40793921&branchId=1303e9b3-0978-47b7-b1c7-7f286697e2fb:11:27)
at ScriptComponent._scriptMethod (https://code.playcanvas.com/playcanvas-stable.js:44394:19)
at ScriptComponent._onUpdate (https://code.playcanvas.com/playcanvas-stable.js:44421:11)
at ScriptComponentSystem._callComponentMethod (https://code.playcanvas.com/playcanvas-stable.js:44889:49)
at ScriptComponentSystem._onUpdate (https://code.playcanvas.com/playcanvas-stable.js:44901:9)
at Function._helper (https://code.playcanvas.com/playcanvas-stable.js:29030:12)
at Function.update (https://code.playcanvas.com/playcanvas-stable.js:29041:9)
at Application.update (https://code.playcanvas.com/playcanvas-stable.js:49905:20)
at Application.tick (https://code.playcanvas.com/playcanvas-stable.js:50509:16)
at Application.start (https://code.playcanvas.com/playcanvas-stable.js:49897:9)

Sounds like you haven’t enabled gamepads in the project settings.

See Gamepad help please

you were right.

@yaustar now it just does nothing

The code in your if statement does nothing so I’m not sure what you were expecting :sweat_smile:

Also I see that you are using a chrome book, I would check the gamepad works on that laptop with https://gamepad-tester.com/

i already did test the game pad does work i was trying to make it so that when i press the button it counts as pressing space.