三.js在物体旋转时旋转相机

时间:2021-01-09 19:08:03

标签: javascript three.js

我的游戏中有一架直升机,我希望摄像机始终注视直升机的后部,但我做不到。当直升机旋转时,我不能让相机随着直升机旋转。这是直升机和相机旋转的图像。This is what I want even the helicopter rotates. But this is what happened \\

相机初始化:const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

function cameraUpdate(camera) {
  camera.position.x = movingObject.x + 20
  camera.position.y = movingObject.y + 20;
  camera.position.z = movingObject.z;
  camera.lookAt(movingObject.position);}

这个函数在我的渲染循环中调用。 movingObject 是我的直升机。直升机的旋转代码在我的 inputController 函数中,该函数在渲染循环中被调用。轮播片段如下。

if (input.rotClk === true) {
    moveableObj.rotatation.y += -0.05;
}

我尝试了 camera.rotation.y += -0.05 但它没有用。

1 个答案:

答案 0 :(得分:1)

你可以让你的直升机成为相机的孩子,然后让你的输入控制器控制相机,它看起来像相机跟随直升机(实际上直升机跟随相机,只是向前和向下平移) 因此,在您的 init 函数中:

camera.add(movingObject);
movingObject.position.set(0,-10,-100);
scene.add(camera)

这个答案可能有更多信息How to put an object in front of camera in THREE.JS?