[SOLVED] Changing possitions of ball not working Screen to world();

Hello guys,

I have a lot of trouble with function screenToWorld. I have simple basket game,where I throwing ball to hoop. After succes throw, the ball will change positions to side and also Camera will move and Rotate to side. But in code, I have camera.screenToWorld function, which is working just in default position, which is on start game. When the ball was moved to side possition and camera also , the screen to World working just in default possition so ball will be throw badly. Is there some code to fix it? I am trying to set up code, where if the ball was moved nad camera also, function screen to World will be repeat. But it doesnt work.
Maybe its not working cause the ball has some X,Y,Z position and after move, where the camera change position and rotation, ball has same X , Y and Z position like in default. I want , that the balls VEC 3 will change with rotate of the camera. Like rotate of the camera in position 2 will be 30 degrees of Y position so Ball should should rotate VEC3 depends of the camera. But the ball has the same VEC 3 like in default .
Here is some code:

BallMovement.prototype.onMouseMove = function(event){
  
    
    
    if(this.isMousePressed ) {
    var context = this;
    var canTeleport = true;
   
        
        
         this.cameraEntity = this.app.root.findByName('Camera');    
   
        if(CameraCollision.changePositions == 1) {
            var depth = 3.8;
                  
             this.cameraEntity.camera.screenToWorld(event.x + 10, event.y + 10,depth, this.pos);
            
            
        }
       else if (CameraCollision.changePositions ==2) {
            
            var depth2 = 3.8;
                  
             this.cameraEntity.camera.screenToWorld(event.x + 10, event.y + 10,depth2, this.pos2);
             
        }
        else if(CameraCollision.changePositions ==3) {
            
            var depth3 = 3.8;
                  
             this.cameraEntity.camera.screenToWorld(event.x + 10, event.y + 10, depth3, this.pos3);
            
        }
    
   
    
        
    
         if(CameraCollision.changePositions ==1 && this.isBallClicked && canTeleport) {
     
        
        this.ball.setLocalPosition(this.pos.x, this.pos.y, this.ball.getLocalPosition().z);
      
         }
        
        else if(CameraCollision.changePositions == 2 && this.isBallClicked && canTeleport) {
            

              this.ball.setLocalPosition(this.pos2.x , this.pos2.y  , this.ball.getLocalPosition().z);
             
        }
         
         else if(CameraCollision.changePositions ==3 && this.isBallClicked && canTeleport) {
            
            
             this.ball.setLocalPosition(this.pos3.x , this.pos3.y  , this.ball.getLocalPosition().z);
            
            
        }
     }
};   

Its not working, cause if ball and camera will move to changed position 2 or 3, it will write to console that it cannot read properties of this.pos2.x and y, and this.pos3.x and y.

If you have some solution, please share with me your knowledge.
Thank you.

Where are this.pos, this.pos2 and this.pos3 defined?

Oh, I forget it. I thought that its will be defined automatically in screentoworld code. Should I defined IT like that?

this.pos2 = new pc. Vec3

this.pos3 = new pc. Vec3 ?

Thanks.

Yes. screentoworld has that optional parameter so that it doesn’t allocate a new pc.Vec3 and will set the result into that parameter.

However, if that parameter is null it can’t do that and will allocate a new pc.Vec3 and return that from the function.

Its working. Thank you so much.