当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to rotate object with camera three.js

问题描述:

Hi I am trying to make an abject follow the camera so I could achieve an effect of a rifle viewfinder. I am using OrbitControl. When I check camera.rotation.y it only shows that the range is from -PI/2 to PI/2 while I can rotate camera by 360 degrees. I cannot get my head around it please help!

so far I got there (it is only a part of my code):

 pivot = new THREE.Object3D();

pivot.add(viewfinder);//adding viewfinder as a child of pivot

scene.add( pivot );

and later on

 pivot.rotation.y = camera.rotation.y;

this allows to rotate my viewfinder but for some reason it is exactly by Pi/2 shifted so I deducted it from current camera position and I have this:

 pivot.rotation.y = (camera.rotation.y - (Math.PI/2)) ;

and that allows me to rotate viewfinder with camera but only in the range of 180 degrees. How can I rotate it by full 360?

Please help me.

Thak You.

网友答案:

I guess you want to make the mesh always face to your camera, right? you could use camera matrix to set mesh rotation, mesh.rotation.setRotationFromMatrix(camera.matrix), but be careful if your mesh had a parent entity. or you can use lookAt() function, mesh.lookAt(camera.position), either works fine.

分享给朋友:
您可能感兴趣的文章:
随机阅读: