How can I hide the walls?

Hello, according to this house plan, I want to hide the wall that the camera sees, but I am getting an error. how can i do?

I wrote the following sample code in the playground below.

// Function to determine the closest wall based on raycast
    function getClosestWall(cameraPosition) {
        const ray = new BABYLON.Ray(cameraPosition, camera.direction); // Create a ray from camera position
        const pickInfo = scene.pickWithRay(ray); // Perform raycast

        if (pickInfo.pickedMesh && pickInfo.pickedMesh instanceof wall) { // Check if it hit a wall
            return pickInfo.pickedMesh; // Return the closest wall
        } else {
            return null; // No wall hit
        }
    }

    scene.registerBeforeRender(() => { // Update on every frame
        const closestWall = getClosestWall(camera.position); // Get the closest wall

        if (closestWall) {
            closestWall.isVisible = false; // Hide the closest wall for this frame
        } else {
            // Make all walls visible if no wall is hit
            for (const wall of walls) {
                wall.isVisible = true;
            }
        }
    });

I think the error is because

LINE 663: const ray = new BABYLON.Ray(cameraPosition, camera.direction);

Replace e.g.

const ray = new BABYLON.Ray(cameraPosition, camera.getForwardRay().direction, 100);

Anyway, there is just a single mesh “custom” in your scene. Therefore " instanceof wall never returns true.

I believe it is possible to shorten to

const ray =  camera.getForwardRay()

Here is not tuned but working example - https://playground.babylonjs.com/#4GBWI5#2326 (at least the mesh now could be picked with ray).

2 Likes

Can you be more precise? The camera sees all walls on the scene:

@Joe_Kerr @labris did you get the question guys? if so, why didn’t I? :see_no_evil: :crazy_face:

The first thing was the error in the console. This is the main question :slight_smile:

1 Like

While I’m waiting to clarify the question by the OP:

Clip planes?

I’m sorry for my bad english. What I mean is, if a ray was drawn from the camera and the wall it hit was invisible. That’s what I meant. I hope I could explain it this time.

Dude, I told you, there are not any wall meshes.

Think diorama

As @Joe_Kerr is saying, @fcozsargin you have only one mesh.

oh sorry! I just realized that the buildfromhouseplan method creates a single mesh, but I figured out how to hide the mesh.

thank you so much its work