最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ThreeJS: rotate on y axis to face camera - Stack Overflow

programmeradmin3浏览0评论

I want my object to rotate on the Y axis to always face the camera (as if a human was turning around in circles), as the camera moves.

This is what I have tried so far:

var render = function() {
    animationFrameId = window.requestAnimationFrame( render);

    obj.lookAt(camera.position);

    obj.rotation.set(0, obj.rotation.y, 0);
}

But i am missing some simple math or trig function, because when i rotate, it eventually 'jumps' and the object appears to face the wrong direction

I want my object to rotate on the Y axis to always face the camera (as if a human was turning around in circles), as the camera moves.

This is what I have tried so far:

var render = function() {
    animationFrameId = window.requestAnimationFrame( render);

    obj.lookAt(camera.position);

    obj.rotation.set(0, obj.rotation.y, 0);
}

But i am missing some simple math or trig function, because when i rotate, it eventually 'jumps' and the object appears to face the wrong direction

Share Improve this question edited Mar 24, 2016 at 14:08 wildwilly asked Mar 24, 2016 at 13:54 wildwillywildwilly 831 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 24

If you use a .lookAt the object look to the camera in all directions, you have to calculate the angle on Y plane between camera and mesh like this.

var render = function() {

    animationFrameId = window.requestAnimationFrame( render);    

    obj.rotation.y = Math.atan2( ( camera.position.x - obj.position.x ), ( camera.position.z - obj.position.z ) );

}
发布评论

评论列表(0)

  1. 暂无评论