PickWithRay's predicate parameter is invalid

I use pickWithRay to monitor whether the mesh is close to ground. When I return the predicate directly to true, I expect to return the first mesh, but it seems to have no effect in reality

   private _floorRaycast(offsetx: number, offsetz: number, raycastlen: number): BABYLON.Vector3 {
        //position the raycast from bottom center of mesh
        let raycastFloorPos = new BABYLON.Vector3(this._player?.position.x ?? 0 + offsetx, this._player?.position.y ?? 0 + 0.5, this._player?.position.z ?? 0 + offsetz);
        let ray = new BABYLON.Ray(raycastFloorPos, BABYLON.Vector3.Up().scale(-1), raycastlen, 0.1);
        const t = Date.now()
        let pickMesh = null

        //defined which type of meshes should be pickable
        let cb = (mesh) => {
            console.log(mesh)
            return true
        }
        console.log("start")
        let pick = this._scene.pickWithRay(ray, cb);
        console.log(pick, "pick")
        console.log("end")


        if (pick?.hit) { //grounded
            return pick.pickedPoint!;
        } else { //not grounded
            return BABYLON.Vector3.Zero();
        }

    }

    //raycast from the center of the player to check for whether player is grounded
    private _isGrounded(): boolean {
        // console.log(this.num++,"_isGrounded")
        if (this._floorRaycast(0, 0, .6).equals(BABYLON.Vector3.Zero())) {
            return false;
        } else {
            return true;
        }
    }```
![image|690x254](upload://58xB2ctm0Zb3b6ofGdODe55znds.png)

Create a PG please.

Your screenshot shows the ray going “through” all of the meshes, including your GroundMesh, which is expected because your predicate is always true.

Try returning false on the first mesh encountered if you want the first mesh?

Playgrounds are more helpful for getting more detailed and helpful responses, but HTH anyhow!

1 Like


No matter what is returned, it has no effect

Not sure what you are trying to do, predicate is a function that every mesh will call, the returned boolean indicates if the mesh can be picked. If you want to get the first mesh you can use fastCheck param, if the closet I’m afraid you should get all meshes picked and sort by distance.

2 Likes