My gltf gun model has several animations and one of them is shooting anim.In the animation a bullet is throwing from gun, but the problem is the bullet hanging on scene after animation ended. goToFrame(0)
doesn’t work.
The code:
// Setup Babylon.js
const canvas = document.getElementById('renderCanvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let engine = new Engine(canvas, true);
let scene = new Scene(engine);
// Initialize Havok
const havok = await HavokPhysics();
const plugin = new HavokPlugin(undefined, havok);
scene.enablePhysics(new Vector3(0, -9.81, 0), plugin);
const text = new CubeTexture('public/environment.env', scene)
const skybox = scene.createDefaultSkybox(text, true, 1000)
// Camera and light
let camera = new FreeCamera('camera1', new Vector3(0, 2.4, -15), scene);
camera.setTarget(Vector3.Zero());
camera.attachControl(canvas, true);
let light = new HemisphericLight('hl', new Vector3(0, 1, 0), scene);
// Ground
let ground = MeshBuilder.CreateGround('ground', {width: 256, height: 256}, scene);
let groundMat = new StandardMaterial('gm', scene);
groundMat.diffuseTexture = new Texture('public/diff.jpg', scene);
groundMat.diffuseTexture.uScale = 36.5;
groundMat.diffuseTexture.vScale = 36.5;
ground.material = groundMat;
const groundAggr = new PhysicsAggregate(ground, PhysicsShapeType.BOX, { mass: 0 }, scene);
let cps = MeshBuilder.CreateCapsule('capsule', {height: 4, radius: 0.5}, scene);
cps.position.y = 2;
cps.position.x = 2;
let box = MeshBuilder.CreateBox('box', {size: 1}, scene);
box.position.y = 0.5;
box.position.x = 1;
cps.checkCollisions = true;
box.checkCollisions = true;
const cpsAggr = new PhysicsAggregate(cps, PhysicsShapeType.CAPSULE, { mass: 1 }, scene);
const boxAggr = new PhysicsAggregate(box, PhysicsShapeType.BOX, { mass: 1 }, scene);
camera.applyGravity = true;
camera.ellipsoid = new Vector3(1.5, 1.2, 1.5);
camera.checkCollisions = true;
ground.checkCollisions = true;
let anm;
SceneLoader.ImportMesh("", "public/", "pistol_fps.glb", scene, (meshes, ps, s, animationGroups) => {
console.log(animationGroups)
anm=animationGroups.find(g=>g.name === 'Armature.003|shooting')
const fpsm = meshes[0];
fpsm.parent = camera;
const k = 1/280;
fpsm.scaling = new Vector3(k, k, k);
fpsm.position = new Vector3(0.8, -0.77, 2.6); // Adjust these values to position the model correctly
});
//play shooting animation on mouse click
scene.onPointerDown = () => {
anm.start(false, 2);
}