ScreenToWorld Zdepth issue


#1

Hello All,
I hope you all are doing fine, please help me I am stuck at one point,
the Z depth in the ScreenToWorld is causing issues. Please check my code.

Issue: The main issue is that the touch event is only detected at a depth of 18, and as you can see when I touch far away on the platform the ball wont move their as the depth is 18 while the camera’s far is 70 and min is 0 as set in the editor.
Please help me in this regard.

// Moving ball using Touch Move
PlayerController.prototype.onTouchMove = function(event){

if(GetTouchPos === true){   // For Panning
    
    if(typeof this.cameraEntity.camera !== 'undefined'){
        
        // Getting position for the ball
        var from = this.entity.getPosition();
        
        // Getting touch position ~ zdepth = 18 causing issues, different values causing different issues.
        // Z depth cauing issue
        this.pos = this.cameraEntity.camera.screenToWorld(event.touches[0].x, event.touches[0].y, 18); // ISSUE here

        // Getting the direction 
        this.Direction.sub2(this.pos,from); 
        
        // Getting the distance 
        this.Distance = this.Direction.length();
        
        // Normalize Vector 
        this.Direction.normalize();
        
        // Making y = 0 so ball does not fly upwards
        this.pos.y=0;

        if(canPlay === true){  // If we are playing the game
            // Using the direction vector to translate the ball towards the touched position.
            this.entity.translate(this.Direction.x*globaldt*20,0,this.Direction.z*globaldt*20);
        }

    }
}

};

The project is here:
https://playcanvas.com/project/636022/overview/ball-with-kinematic

The Build is here:


#2

What you will need to do is to project a ray from the camera to (and through) the world point from screenToWorld to check for intersection against a plane that is level with the ball.

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


#3

Yes I got the code working perfectly!
Thank you yaustar! Really appreciate the help.