因此,我希望使用lookAt函数,因为我希望僵尸朝向角色前进。问题似乎是它甚至无法正确旋转,但角度怪异。这就是我所做的:
var pos =新的THREE.Vector3(self.position.x-player.x, self.position.y,self.position.z-player.z)self.lookAt(pos)
是的,这是什么问题?我有一个理论,我需要以某种方式获得球员的相对位置,但是我不知道该怎么做。
刚刚尝试
lookAt(Player)
答案 0 :(得分:1)
如果僵尸和玩家都没有任何超出您应能使用的范围的父母
zombieMesh.lookAt(playerMesh.position);
请注意,lookAt
使模型的正Z轴指向目标。
'use strict'; // eslint-disable-line
/* global THREE, document, requestAnimationFrame */
function main() {
const canvas = document.querySelector('#c');
const renderer = new THREE.WebGLRenderer({canvas: canvas});
const fov = 60;
const aspect = 2; // the canvas default
const zNear = 0.1;
const zFar = 1000;
const camera = new THREE.PerspectiveCamera(fov, aspect, zNear, zFar);
camera.position.set(14, 8, 6);
camera.lookAt(-2, -2, 0);
const scene = new THREE.Scene();
{
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(0, 20, 0);
scene.add(light);
}
{
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(1, 2, 4);
scene.add(light);
}
const groundGeometry = new THREE.PlaneBufferGeometry(50, 50);
const groundMaterial = new THREE.MeshPhongMaterial({color: 0xCC8866});
const groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
groundMesh.rotation.x = Math.PI * -.5;
groundMesh.receiveShadow = true;
scene.add(groundMesh);
const zombieGeo = new THREE.ConeBufferGeometry(1, 1, 6);
zombieGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI * 0.5));
const zombieMat = new THREE.MeshPhongMaterial({color: 'green', flatShading: true});
const zombieMeshes = [];
function makeZombie(x, z) {
const zombieMesh = new THREE.Mesh(zombieGeo, zombieMat);
scene.add(zombieMesh);
zombieMesh.position.set(x, 1, z);
zombieMeshes.push(zombieMesh);
}
for (let v = -5; v <= 5; v += 5) {
makeZombie(v, -10);
makeZombie(v, 10);
makeZombie(-5, v);
makeZombie( 5, v);
}
const playerGeo = new THREE.SphereBufferGeometry(1, 6, 4);
const playerMat = new THREE.MeshPhongMaterial({color: 'red', flatShading: true});
const playerMesh = new THREE.Mesh(playerGeo, playerMat);
scene.add(playerMesh);
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render(time) {
time *= 0.001;
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
playerMesh.position.set(Math.sin(time) * 6, 1, Math.cos(time) * 6);
for (const zombieMesh of zombieMeshes) {
zombieMesh.lookAt(playerMesh.position);
}
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
main();
body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/99/three.min.js"></script>
<canvas id="c"></canvas>
答案 1 :(得分:0)
它看起来像是第一人称游戏,所以我认为您可以使用一些棘手的工作...
select [Time],
[ID]
('UID-' + FORMAT([Time], 'yyyyMMddHHssmmm') + '-' + ID) as UID
from YourTable
从本质上讲,因为您始终可以通过相机获得“眼睛”,所以无论“眼睛”在哪里,僵尸都可以跟随您。该函数说要更改僵尸位置,并且事件侦听器正在侦听摄像头更改。
我没有看到任何示例代码,但是请确保您的lookAt语句在对象的范围内!我对发生在我身上的事情感到内......
答案 2 :(得分:-1)
在ThreeJS中使用lookAt
时,它很聪明,可以根据对象的位置自动进行计算,但是您的代码失败了,因为您尝试计算对象周围的方向,应该在哪里看
只需使代码更简短:
self.lookAt(player);