I'm currently facing challenges while developing a first-person shooter game using Three.js. I've encountered major glitches with THREE.SceneUtils.detach()
and THREE.SceneUtils.attach()
, and I'm uncertain if they are the appropriate methods to use.
In my game, my mech character has two guns positioned on either side. There is enough space between the guns for a small enemy to pass through without getting shot. To address this issue, I decided to make the guns 'lock on' and follow an enemy when it enters the center of the camera view. However, implementing this required me to refer to WestLangley's concept mentioned here: Three.js Rotate objects inside of moving Object3D to always face the camera
As a result, the revised portion of my code now looks like this:
autoTarget: function ( target ) {
THREE.SceneUtils.detach( gunRootObject, characterRootObj, scene );
gunRootObject.lookAt( target );
gunRootObject.updateMatrix();
this.direction = new THREE.Vector3( 0, 0, 1 );
this.direction.applyQuaternion( gunRootObject.quaternion );
THREE.SceneUtils.attach( gunRootObject, scene, characterRootObj);
}
Although this solution works successfully in half of the attempts, the other half results in both guns getting stuck at [0,0,0] concerning the characterRootObj
.
Would you be able to point out any evident mistakes in my implementation? Is there a better approach to resolve the original problem I described?
Your suggestions and insights would be highly appreciated. Thank you!