[SOLVED] Touch input world space

I like to start with making my game mobile playable.

The first touch inputs works oke, but only with specific camera perspective.

In the example below the player looks at touch input only below half of the screen.
So touch input above (or the back) of the player doesn’t work.

I use this code right now:

Player.prototype.updateFromScreen = function (screenPosition) {
    var depth = 9;
    this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, depth, this.touchPosition);
    
    // Rotate the player to touch input
    this.moveDirection = new pc.Vec3(this.touchPosition.x, player.getPosition().y, this.touchPosition.z);
};


Player.prototype.onTouchStart = function (event) {
    if (event.touches.length === 1) {
        this.updateFromScreen(event.touches[0]);
        //playerIsMoving = true;
    }
};

Player.prototype.onTouchMove = function (event) {
    this.updateFromScreen(event.touches[0]);
};


Player.prototype.onTouchEnd = function (event) {
    if (event.touches.length === 0) {
        //playerIsMoving = false;
    }
};

Player.prototype.onTouchCancel = function (event) {
    if (event.touches.length === 0) {
        //playerIsMoving = false;
    }
};

Also selecting an inventory item is not working as expected but maybe thats a problem for later.

The depth of the screen to world is set to 9 units at the moment in your code so the point in space is always 9 units away from the camera.

I would use a raycast method instead for this.

Here’s an example https://developer.playcanvas.com/en/tutorials/point-and-click-movement/

1 Like

Change the the depth is not working but with using the example project it’s working now. So thank you!