intersectsMesh Babylon.js

I am creating a project in which it is necessary to return an object to certain coordinates when it intersects with another object

Here’s the source code, what’s wrong?

Thanks!

`const createScene = () => { const scene = new BABYLON.Scene(engine);

const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 3, new BABYLON.Vector3(0, 0, 0));
camera.attachControl(canvas, true);

const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0));
const box = BABYLON.MeshBuilder.CreateBox("box", {});

pointerDragBehavior2 = new BABYLON.PointerDragBehavior({ dragPlaneNormal: new BABYLON.Vector3(0, 1, 0) });
pointerDragBehavior2.useObjectOrientationForDragging = false;

box.addBehavior(pointerDragBehavior2);


const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {});
sphere.position.x = 5;

if (box.intersectsMesh(sphere, false)) {
    box.position.x = 0;
    box.position.y = 0;
    box.position.z = 0;
}

return scene;

}`

I tried to do it with this code, but it didn't work out, what am I doing wrong?

Welcome to the community,

This will only check the intersection once are you trying to constantly check for every frame ?

You could share the code in the playground to make it easier for us to help you :slight_smile:

I try every time
https://playground.babylonjs.com/#DCCVXY

This does not do it every time, only once when you start.

Your code needs to be in the onBeforeRenderObservable of the scene to run before every frame.

2 Likes