GLB object always facing camera

Is it possible glb object can always facing camera position?
I import character model glb to scene, and I want the character always facing to camera.

you can use gltf.scene.lookAt(camera.position) in your update function

hi forerunrun, this is my import glb code

assetLoader.load(logoUrl.href, function (gltf) {
const logo = gltf.scene;
logo.position.set(0, 3, 0);
logo.traverse(function (node) {
if (node.isMesh)
node.castShadow = true;
node.receiveShadow = true;
})
scene.add(logo);
});

const tick = () => {

logo.scene.lookAt(camera.position)

};

tick();

and this is the error result :
Uncaught ReferenceError: logo is not defined

Try like this…

let logo
assetLoader.load(logoUrl.href, function (gltf) {
logo = gltf.scene;
logo.position.set(0, 3, 0);
logo.traverse(function (node) {
if (node.isMesh)
node.castShadow = true;
node.receiveShadow = true;
})
scene.add(logo);
});

const tick = () => {

logo.lookAt(camera.position)

window.requestAnimationFrame(tick)

};

tick();

logo needs to be defined globally to access it in the tick function…

i have tried and now have another problem

‘Uncaught TypeError: Cannot read properties of undefined (reading ‘lookAt’)’

That’s likely because your tick function is called before your logo asset is loaded, you can either 1. call tick from inside your loader function, or 2. use a conditional statement inside your tick function eg…

if (logo) {
 logo.lookAt()
}

Otherwise you could use three’s LoadingManager to run functions once all loading is finished…

1 Like