【问题标题】:Three.js rotate the camera when the object rotate三.js在物体旋转时旋转相机
【发布时间】:2021-04-15 04:43:48
【问题描述】:

我的游戏中有一架直升机,我希望摄像机始终注视直升机的后部,但我做不到。当直升机旋转时,我无法让相机随直升机旋转。这是直升机和相机旋转的图像。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 函数中,该函数在渲染循环中调用。旋转sn-p如下。

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

我尝试 camera.rotation.y += -0.05 并没有成功。

【问题讨论】:

    标签: javascript three.js


    【解决方案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?

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 2016-09-28
      • 2015-01-30
      • 2013-09-10
      • 2020-05-10
      • 2014-01-27
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多