Graphics 三发子弹

Graphics 三发子弹,graphics,three.js,Graphics,Three.js,我是three.js的初学者。我的任务是建立一个简单的FPS游戏。我的枪和子弹有很多问题。当我按下“空格键”时,我的武器会射击,但问题是子弹只在屏幕的一小部分朝着正确的方向移动,然后它们开始朝着我不想要的方向移动 这是一个例子: 以下是我为bullet编写的代码: // SHOOT BULLET for(var index=0; index<bullets.length; index+=1){ if( bullets[index] === undefin

我是three.js的初学者。我的任务是建立一个简单的FPS游戏。我的枪和子弹有很多问题。当我按下“空格键”时,我的武器会射击,但问题是子弹只在屏幕的一小部分朝着正确的方向移动,然后它们开始朝着我不想要的方向移动

这是一个例子:

以下是我为bullet编写的代码:

// SHOOT BULLET




for(var index=0; index<bullets.length; index+=1){

            if( bullets[index] === undefined ) continue;
            if( bullets[index].alive == false ){
            bullets.splice(index,1);
            continue;
            }

            bullets[index].position.add(bullets[index].velocity);
            }

            if(keyboard[32] && canShoot <= 0){ // spacebar key

            // creates a bullet as a Mesh object
            var bullet = new THREE.Mesh(
                                        new THREE.SphereGeometry(0.2,8,8),
                                        new THREE.MeshBasicMaterial({color:0x42FFFF})
                                        );
            // position the bullet to come from the player's weapon
            bullet.position.set(
                                camera.position.x - 0.7*parseInt(-Math.cos(camera.rotation.z)),
                                camera.position.y - 0.3,
                                camera.position.z +1*parseInt(-Math.cos(camera.rotation.z))
                                );

            // set the velocity of the bullet
            bullet.velocity = new THREE.Vector3( (-mouse.x - Math.sin(camera.rotation.y + Math.PI/6) * 7),//*parseInt(-Math.cos(camera.rotation.z)) ,
                                                mouse.y,
                                                Math.cos(camera.rotation.y)*parseInt(-Math.cos(camera.rotation.z))
                                                                                                    ).normalize();
                console.info(bullet.velocity);
            // after 1000ms, set alive to false and remove from scene
            // setting alive to false flags our update code to remove
            // the bullet from the bullets array
            bullet.alive = true;
            setTimeout(function(){
                       bullet.alive = false;
                       scene.remove(bullet);
                       }, 1000);
            // add to scene, array, and set the delay to 10 frames
            bullets.push(bullet);
            scene.add(bullet);
            canShoot = 10;
            }
            if(canShoot > 0) canShoot -= 1;
//射出子弹

对于(var index=0;index如何设置子弹方向和移动的粗略概念:

var scene=new THREE.scene();
var摄像机=新的三透视摄像机(60,window.innerWidth/window.innerHeight,0.0110000);
摄像机位置设置(0,0,1);
场景。添加(摄影机);
var renderer=new THREE.WebGLRenderer({
反别名:对
});
renderer.setSize(window.innerWidth、window.innerHeight);
document.body.appendChild(renderer.doElement);
var controls=新的三个.OrbitControls(摄影机、渲染器.doElement);
var background=新的三网格(新的三网格球面测量法(1000,90,45)),新的三网格基本材质({
颜色:“灰色”,
线框:正确
}));
场景。添加(背景);
var武器=新的THREE.Mesh(新的THREE.BoxGeometry(1,1,5)),新的THREE.MeshBasicMaterial({
颜色:0x5555ff
}));
武器。位置。设置(2,-1,-2.5);
相机。添加(武器);
var emitter=new THREE.Object3D();
发射器位置设置(2,-1,-5);
添加(发射器);
var浆细胞=[];
addEventListener(“mousedown”,onMouseDown);
函数onMouseDown(){
让Plasmarball=新的3.Mesh(新的3.SphereGeometry(0.5,8,4)),新的3.Mesh基本材料({
颜色:“浅绿色”
}));
plasmaBall.position.copy(emitter.getWorldPosition());//起始位置-武器尖端
plasmaBall.quaternion.copy(camera.quaternion);//应用相机的四元数
场景。添加(等离子球);
浆板球。推(浆板球);
}
无功转速=50;
var clock=新的三个时钟();
var-delta=0;
(函数render(){
请求动画帧(渲染);
delta=clock.getDelta();
浆细胞。forEach(b=>{
b、 translateZ(-speed*delta);//沿局部z轴移动
});
渲染器。渲染(场景、摄影机);
})()
正文{
溢出:隐藏;
保证金:0;
}

关于如何设置子弹方向和移动的粗略概念:

var scene=new THREE.scene();
var摄像机=新的三透视摄像机(60,window.innerWidth/window.innerHeight,0.0110000);
摄像机位置设置(0,0,1);
场景。添加(摄影机);
var renderer=new THREE.WebGLRenderer({
反别名:对
});
renderer.setSize(window.innerWidth、window.innerHeight);
document.body.appendChild(renderer.doElement);
var controls=新的三个.OrbitControls(摄影机、渲染器.doElement);
var background=新的三网格(新的三网格球面测量法(1000,90,45)),新的三网格基本材质({
颜色:“灰色”,
线框:正确
}));
场景。添加(背景);
var武器=新的THREE.Mesh(新的THREE.BoxGeometry(1,1,5)),新的THREE.MeshBasicMaterial({
颜色:0x5555ff
}));
武器。位置。设置(2,-1,-2.5);
相机。添加(武器);
var emitter=new THREE.Object3D();
发射器位置设置(2,-1,-5);
添加(发射器);
var浆细胞=[];
addEventListener(“mousedown”,onMouseDown);
函数onMouseDown(){
让Plasmarball=新的3.Mesh(新的3.SphereGeometry(0.5,8,4)),新的3.Mesh基本材料({
颜色:“浅绿色”
}));
plasmaBall.position.copy(emitter.getWorldPosition());//起始位置-武器尖端
plasmaBall.quaternion.copy(camera.quaternion);//应用相机的四元数
场景。添加(等离子球);
浆板球。推(浆板球);
}
无功转速=50;
var clock=新的三个时钟();
var-delta=0;
(函数render(){
请求动画帧(渲染);
delta=clock.getDelta();
浆细胞。forEach(b=>{
b、 translateZ(-speed*delta);//沿局部z轴移动
});
渲染器。渲染(场景、摄影机);
})()
正文{
溢出:隐藏;
保证金:0;
}


谢谢您的帮助。我想编辑我的代码,了解我失败的地方并尝试更正它。如果我不能,我将使用此代码段。谢谢您的帮助。我想编辑我的代码,了解我失败的地方并尝试更正它。如果我不能,我将使用此代码段。