Need help with raycast, the array is empty

I want to do raycasting but the pickableShapes is empty

C1.prototype.doRayCast = function (screenPosition) {
    console.log(this.cameraEntity.getPosition());
    
    // Initialise the ray and work out the direction of the ray from the a screen position
    this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.cameraEntity.camera.farClip, this.ray.direction); 
    this.ray.origin.copy(this.cameraEntity.getPosition());
    this.ray.direction.sub(this.ray.origin).normalize();

    console.log(this.pickableShapes);
    
    // Test the ray against all the objects registered to this picker
    for (var i = 0; i < this.pickableShapes.length; ++i) {
        var pickableShape = this.pickableShapes[i];
        var result = pickableShape.intersectsRay(this.ray, this.hitPosition);

        
            console.log(pickableShape);
        
    }    
};

Hi @grzesiekmq,

If you are trying to use the Entity Picking without physics example, then you have to get all the relevant code from the shape-picker.js script.

The pickableShapes array is indeed empty on its own, you will have to fill it with the shapes that will be tested for picking.

Check the code/sample from the tutorials page on how to do that:

https://playcanvas.com/editor/code/436809?tabs=5718172

https://playcanvas.com/editor/scene/474338

1 Like

Iā€™m looking for solution similar to entity picking but do the raycasting in the model to look for the MeshInstance when the ray is intersected

You will have to extend the example above to create one shape for each mesh instance, then it will work.

1 Like