我想要实现的是让特定的网格体向特定的向量移动,直到它最终被玩家停止。
到目前为止,我已经成功获取了单击 Canvas 的 XY 坐标,并使用以下代码将它们投影到 3D 中。不幸的是,我不确定采取什么方法才能获得指向点击位置的方向。
var vector = new THREE.Vector3();
vector.set(
( event.clientX / window.innerWidth ) * 2 - 1,
+ ( event.clientY / window.innerHeight ) * 2 + 1,
0.5 );
vector.unproject( camera );
var dir = vector.sub( camera.position ).normalize();
var distance = + camera.position.z / dir.z;
var pos = camera.position.clone().add( dir.multiplyScalar( distance ) );
最佳答案
这假设目标 Vector3 和每帧移动的最大距离为 0.01。
var vec1 = target.clone(); // target
vec1.sub(mesh.position); // target - position
var dist = Math.min(vec1.length(), .01); // assume .01 is maximum movement
if (dist > 0) {
vec1.setLength(dist); // this will be the movement
mesh.position.add(vec1); // this moves the messh
}
关于javascript - ThreeJS,获取指向点击方向的向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37447882/