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.

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;
    }
}

ok thanks.

hey @yaustar while i have your attention how would i make the mouse follow the right stick? if I can. also how would i make R2 count as a click.

@Leonidas can you help with this?

I dont know how to do that but if there was something that you make an object and control it with the gamepad axis you could put the mouse position on the object that the axis is controlling. Is there anything like that in playcanvas?. I think there is.

For the second question there is a function called fire(); so that you can make fire be a mouse click and a gamepad press at the same time. Like fire = "//put the mouse click and gamepad press here"; So you can then add if(fire){//add your function here}

I hope it helps @WilliamBoersma31.

Hey so this thread has been super helpful and I was able to get real time data from my ds4 controller working fine. The only issue is that the L2 and R2 triggers seem to be treated as buttons and I cannot get their axis values. Is this a limitation or am I just doing something wrong? Here is my playcanvas project for this https://playcanvas.com/project/797941/overview/controllerinputtest.

One other issue that I’m facing is that I can’t seem to get my controller detected in firefox. I tested on chrome and edge and both work fine. I also had similar results with gamepad api https://gamepad-tester.com/.

Unfortunately, our game pad API doesn’t handle analogue buttons so this would need to be a feature request.

In the meantime, you can use the browser gamepad API directly as PlayCanvas only does a lightweight wrapper around it: https://developer.mozilla.org/en-US/docs/Web/API/Gamepad or use a third party library: https://samiare.github.io/Controller.js/

I’m afraid that’s an issue with the browser if other game testers are having the same issue.

1 Like