Move 3D imported model

Hello guys, I’m having some trouble in my initial project with Babylon.js.
I have imported my model 3D at the project, as I show bellow:

tank = BABYLON.SceneLoader.ImportMesh("","",“Models/model003.babylon”,
scene,function(newMeshes) {
newMeshes.forEach(function(mesh){
mesh.rotation = new BABYLON.Vector3(BABYLON.Tools.ToRadians(
10),0,0);
} );
});

So far so good but now I want to add buttons in order to translate the object.
I’ve tried this:

scene.registerBeforeRender(function(){
if (bla==1){
var posX = Math.sin(tank.rotation.y);
var posZ = Math.cos(tank.rotation.y);
console.log(posX,posZ);
tank.position.x += posX;
tank.position.z += posZ;
}
if(bla == 2){
tank.rotation.y = tank.rotation.y + 0.1;
}
if(bla == 3){
tank.rotation.y = tank.rotation.y - 0.1;
}
});

But the console shows the error “0: Unable to get property ‘y’ of undefined or null reference” so… what should I do ?

Hello and welcome to the forum !!!

ImportMesh does return the mesh but pass it through the callback so

tank = BABYLON.SceneLoader.ImportMesh...

will set tank to undefined.

You could either run this:

scene.registerBeforeRender(function(){
...
}

as part of the callback or rely on the async version to use promises.

Hope that helps.

I really appreciate your answer, thank you.

Could you give me some exemple ?

https://www.babylonjs-playground.com/#JUKXQD#232

should do.

1 Like

It was very helpful, thank you sir :+1:

1 Like