How to put an object in front of camera in THREE.JS?

后端 未结 4 1630
野性不改
野性不改 2021-02-09 03:47

I\'m trying to put an object in front of the camera, but have not been able to.

I\'m using the FlyControls what moves the camera, and I now want to put an object in fron

4条回答
  •  终归单人心
    2021-02-09 04:01

    Here's another method - applying the quarternion of the camera to a vector (dist is how far away from the camera you want the object to be):

    var vec = new THREE.Vector3( 0, 0, -dist );
    vec.applyQuaternion( camera.quaternion );
    
    nanobot.position.copy( vec );
    

    Making the object a child of the camera didn't work for me, but this applyQuarternion method did (adapted from @WestLangley's answer here: Three.js: Get the Direction in which the Camera is Looking)

    You may wish to copy the rotation from the camera too, if you want it to always be facing the camera. And if you want to tweak the object's exact position after this, you can translateX, translateY etc.

提交回复
热议问题