三.js 敌人向玩家移动



在三个.js我在xyz有一艘宇宙飞船,我喜欢它飞向xyz行星的网格物体。

我一辈子都想不通这一点。

需要以恒定的速度向地球直线行进。

 updateFcts.push(function(delta, now){
    if (shipArr[0]===undefined){
}else{  
       //create two vector objects
var xd = new THREE.Vector3(marsMesh.position.x,marsMesh.position.y,marsMesh.position.z);
var yd = new THREE.Vector3(shipArr[0].position.x,shipArr[0].position.y,shipArr[0].position.z);
//find the distance / hypotnuse to the xyz location
var dicks = shipArr[0].position.distanceTo(marsMesh.position);
 var subvec = new THREE.Vector3();
     subvec = subvec.subVectors(xd,yd);
     //sub subtrac the 3 vectors.
   var hypotenuse = dicks;
     console.log(hypotenuse);
   //1.5 stops it at 1.5 distance from the target planet

       if(hypotenuse > 1.5){
        //console.log(hypotenuse);
    shipArr[0].position.y += .0001*200*(subvec.y/hypotenuse);
    shipArr[0].position.x += .0001*200*(subvec.x/hypotenuse);
    shipArr[0].position.z += .0001*200*(subvec.z/hypotenuse);
    }else{
    //within fire range
    alert ("FIIIIIRE");
    }

}
})

我尝试了补间.js但不满意,所以我自己编写了一个函数。

你可以

使用专注于此的 https://github.com/sole/tween.js。

一个非常基本的示例 http://jsfiddle.net/qASPe(正方形将在 5 秒后飞向球体),主要包含以下代码:

new TWEEN.Tween(ship.position)
    .to(planet.position, 700) // destination, duration 
    .start();

稍后,您可能希望使用 THREE。曲线或其他路径机制,作为"飞行"路径,如下所示 http://jsfiddle.net/aevdJ/12

// create a path
var path = new THREE.SplineCurve3([
    ship.position,
    // some other points maybe? representing your landing/takeoff trajectory
    planet.position
]);
new TWEEN.Tween({ distance:0 })
    .to({ distance:1 }, 3000) // destination, duration 
    .onUpdate(function(){
        var pathPosition = path.getPointAt(this.distance);
        ship.position.set(pathPosition.x, pathPosition.y, pathPosition.z);
    })
    .start();

在所有情况下,不要忘记在更新功能中添加此行

TWEEN.update();

最新更新