Search code examples
javascriptthree.jsaframe

A-frame screen click instead of "w" press to walk


i am currently exploring the wonderful world of A-frame. i have added the wasd controls (which works great) however id like to replicate the function of the w button when clicking the screen, so basic vr headsets can also walk around, (think google cardboard with the one little button on the top), ive done some googling and cant seem to find anything, i also wondered is it possible to simulate the w key being pressed when clicking,(random idea, i know) but as always any help is greatly appreciated


Solution

  • If you want to reuse the wasd-controls you can tell it that w is pressed, with a simple hack (original source):

    (wasd-controls-reference).keys['KeyW'] = true // or false
    

    which could be toggled on touchstart/touchend like:

    document.body.addEventListener("touchstart", () => {
      const controlsElement = document.querySelector("[wasd-controls]");
      const controls = controlsElement.components["wasd-controls"];
      controls.keys['KeyW'] = true;
    })
    

    Check it out here.