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
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.
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
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.
The function you are calling is isPressed
on the keyboard object which returns a true/false value.
It doesnât simulate a key press.
Call the function/logic that is executed when space is pressed instead.
yeah i fixed thanks.
ok but how do i use -y and -x on the axis as a input like i can positive x and y?
I donât understand what you mean?
@yaustar like if i use pc.PAD_1, pc.PAD_L_STICK_X that works but if i use pc.PAD_1, pc.PAD_L_STICK_-X That does not work.
pc.PAD_L_STICK_X
returns a value of -1 to 1
-1 being left, 1 being right
so if were to do something like this
var x = gamepads.getAxis(pc.PAD_1, pc.PAD_L_STICK_X);
if (x = 1) {
right = true;
}
if (x = -1){
left = true;
}
if (x = 0) {
left = false;
right = false;
}
if (right) {
entity.move right (not real code)
}
if (left) {
entity.move left (not real code)
}
Would that work?
More like this as someone may have a dodgy joypad that doesnât reach the extremes
var x = gamepads.getAxis(pc.PAD_1, pc.PAD_L_STICK_X);
var left = false;
var right = false;
if (x !== false) {
if (x >= 0.5) {
right = true;
}
if (x <= -0.5) {
left = true;
}
}