I am trying to make a gun for an FPS game. I can make a shape that follows you around when you move, like you are holding a gun. Here is the code for that:
let gun = BABYLON.MeshBuilder.CreateCylinder("rl", {diameterTop: 0.8, diameterBottom: 0.9, height: 3, tessellation: 64}, scene);
gun.renderingGroupId = 1;
gun.material = new BABYLON.StandardMaterial("rlMat", scene);
gun.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
gun.rotation.x = Math.PI/2;
gun.parent = camera;
gun.position = new BABYLON.Vector3(1, -2, 5);
But that is kind of plain. So I am trying to add a OBJ gun model to make it look better.
Here is what I have so far:
let gun = BABYLON.SceneLoader.ImportMesh("gun",
"https://dl.dropbox.com/s/4x3e5136opsm9el/", "m1911-handgun.obj", scene,);
gun.renderingGroupId = 1;
gun.material = new BABYLON.StandardMaterial("rlMat", scene);
gun.rotation.x = Math.PI/2;
gun.parent = camera;
gun.position = new BABYLON.Vector3(1, -2, 5);
If you try to open the URL in the browser, it won’t work because I changed it according to the docs here: Using External Assets In the Playground | Babylon.js Documentation.
But I tried the link on a different playground, so I know that it really does work.
But the gun is not there. It gives me this errorr: cannot set property of x to undefined. Please help.
Thanks for any help in advance!
it is because importMesh does not return a mesh and you either need to use importMeshAsync and await it or put you gun manipulation code in the importMesh onsuccessCallback parameter.