Yo @Deltakosh or the other Dave (I dont see his @ sign tag)
Are we purposely ignoring the PS4 controllers in the Gamepad API. I cant seem to find that part.
But for some reason i can see the PS4 controller using regular HTML Gamepad API in a non babylon project and the HTML Gamepad Test site that reports all the same key values as a Xbox One controller… But i don’t get any response for the PS4 controller in my Babylon Projects… Any ideas as to why that is ???
Well this is because I have no PS4 controller:) But if it behaves like the XBox controller it should be dead easy to support it (we are probably filtering by driver name)
@trevordev helped me come up with the code to handle pretty much any controller…
One second let me dig that up. I gotta clean it out cause its for a specific project I’m not trying to share yet. but the controls work with any controller I believe.
var gamepadManager = new BABYLON.GamepadManager()
scene.gamepadManager = gamepadManager
var gamepad, controls
controls = null
gamepadManager.onGamepadConnectedObservable.add((gamepad, state)=>{
gamepad = gamepadManager.gamepads[0]
if(gamepad){
controls = new Controller(gamepad,
{
sensitivity : 0.3,
deadzone : 0.12,
LYinvert : -1,
LXinvert : 1,
RYinvert : -1,
RXinvert : 1,
}, scene)
}
})
gamepadManager.onGamepadDisconnectedObservable.add((gamepad, state)=>{
gamepad = false
controls = null
console.log('Detached Controller!')
})
scene.onBeforeRenderObservable.add(()=>{
if(controls == null || !controls.gamepad.browserGamepad.buttons){
return
}
if(controls !== null){
controls.update()
}
})
class Controller{
constructor(gamepad, options, scene){
this._options = options
this._scene = scene
this._gamepad = gamepad
console.log(gamepad, "- gamepad connected.")
this._sticksRaw = null
this._sticksSmoothed = null
}
update(){
//console.log(this.gamepad)
var sticksRaw = [
this.gamepad.leftStick.x*this.options.LXinvert,
this.gamepad.leftStick.y*this.options.LYinvert,
this.gamepad.rightStick.x*this.options.RXinvert,
this.gamepad.rightStick.y*this.options.RYinvert,
]
//for debuging once in production just overwrite?
var sticksSmoothed = []
//Apply Deadzones
for(let i=0; i<sticksRaw.length; i+=2){
let sv = new BABYLON.Vector2(sticksRaw[i], sticksRaw[i+1]).scale(this.options.sensitivity)
if(sv.length() < this.options.deadzone){
sv = BABYLON.Vector2.Zero()
}else{
sv.scale((sv.length() - this.options.deadzone) / (1 - this.options.deadzone))
}
sticksSmoothed.push(sv.x, sv.y)
}
this.sticksRaw = sticksRaw
this.sticksSmoothed = sticksSmoothed
}
get gamepad(){return this._gamepad}
get options(){return this._options}
get target(){return this._target}
get camera(){return this._camera}
get sticksRaw(){return this._sticksRaw}
set sticksRaw(v){this._sticksRaw = v}
get sticksSmoothed(){return this._sticksSmoothed}
set sticksSmoothed(v){this._sticksSmoothed = v}
get LX(){return this._sticksSmoothed[0]}
get LY(){return this._sticksSmoothed[1]}
get RX(){return this._sticksSmoothed[2]}
get RY(){return this._sticksSmoothed[3]}
get button(){return this.gamepad._buttons}
}
Prolly needs to be cleaned up some… but this should get you there.