How to align model with camera

How do i rotate my model to look at the camera ??

When i load I came like this:

And I need to rotate manualy

I tryed :

var camera = new BABYLON.ArcRotateCamera(“Camera”, Math.PI / 2, Math.PI / 2, 2, new BABYLON.Vector3(0,0,5), scene);
camera.attachControl(canvas, true);
BABYLON.SceneLoader.Append(“/models/scifi_girl_v.01/”, “scene.gltf”, scene, function (meshes) {
console.log(meshes.meshes[0])
camera.target = meshes.meshes[1];
scene.createDefaultCameraOrLight(true, true, true);
scene.createDefaultEnvironment();
});

Hi

In 3D objects, you have to make all of your objects into a single group for Rotate operation. Once you’ve designed all the mixed objects you’ve designed into a single group, you can do the following:

BABYLON.SceneLoader.Append("/models/scifi_girl_v.01/", “scene.gltf”, scene, function (meshes) {
    
    //This line assigns your master group to a variable.
    var model = meshes[0]
    //This line gives the main group you create, 90 degrees rotation on the y axis.
    model.rotate.y = MATH.PI/2    
});

You want another solution? Then you can change the position of the camera and set the target again. So bring the camera in front of the model. Then give the camera the model as the target.

One of these two solutions will help you.
If this isn’t the solution for you, don’t hesitate to report it :")

Hello, thanks for help.

This dont work

My code :

<script>
        var canvas = document.getElementById("renderCanvas"); // Get the canvas element 

        if(BABYLON.Engine.isSupported()){
            var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine

            // Create the scene space
            var scene = new BABYLON.Scene(engine);
            
            //set black blackground
            // scene.color = new BABYLON.Color3.White()

            // var skybox = BABYLON.Mesh.CreateBox("skyBox", 100.0, scene);
            // var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
            // skyboxMaterial.backFaceCulling = false;
            // skyboxMaterial.disableLighting = true;
            // skybox.material = skyboxMaterial;

            // Add a camera to the scene and attach it to the canvas
            var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 2, new BABYLON.Vector3(0,0,5), scene);

            // mouse controls
            camera.attachControl(canvas, true);

            // Add lights to the scene
            var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene); //most realystic ligth
            var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);

            BABYLON.SceneLoader.Append("/models/scifi_girl_v.01/", "scene.gltf", scene, function (meshes) {
    
                //This line assigns your master group to a variable.
                var model = meshes[0]
                //This line gives the main group you create, 90 degrees rotation on the y axis.
                model.rotate.y = MATH.PI/2    
            });

            // Register a render loop to repeatedly render the scene
            engine.runRenderLoop(function () { 
                scene.render();
            });

            // Watch for browser/canvas resize events
            window.addEventListener("resize", function () { 
                engine.resize();
            });
        } else {
            alert("WebGl não suportado")
        }

Actually it is:
model.rotation.y = MATH.PI/2

1 Like

Error.

https://www.babylonjs-playground.com/#WGZLGJ#541

Whenever there is a cryptic error like that where a callback is eating the specific error, the trick is to put a try/catch in the callback like this:

https://www.babylonjs-playground.com/#WGZLGJ#542

That will reveal the typo of MATH.PI (should be Math.PI) in a way that we can find it in the console.

Also the desired mesh is probably scene.meshes[1] (not [0]).

3 Likes

Thats it, thanks for the help :).

works