Jump to content

Getting the camera to center on model


freetoplay
 Share

Recommended Posts

Doesn't seem to work for me. This is what my code looks like:

var createScene = function () {
        
    // create a basic BJS Scene object
    var scene = new BABYLON.Scene(engine);

    // loader
    var loader = new BABYLON.AssetsManager(scene);
    
    // light
    var light0 = new BABYLON.HemisphericLight("Hemi0", new BABYLON.Vector3(0, 0, 0), scene);

    light0.specular = new BABYLON.Color3(0, 0, 0);

    // cam
    // Parameters : name, position, scene
    var camera = new BABYLON.UniversalCamera("UniversalCamera", new BABYLON.Vector3(0, 0, 10), scene);

// Targets the camera to a particular position. In this case the scene origin
    camera.setTarget(BABYLON.Vector3.Zero());

// Attach the camera to the canvas
    camera.attachControl(canvas, true);
    
    
	var model = loader.addMeshTask("box", "", "./", "character.glb");
    
    loader.onFinish = function(task) {
        camera.setTarget(task[0].loadedMeshes[0].position);
       
        engine.runRenderLoop(function () {
            scene.render();
        });
    };

    
    loader.load();
    
    return scene;

};

 

Link to comment
Share on other sites

Recreated your code as a playground and it seems to work fine? 

https://www.babylonjs-playground.com/#ZJYNY#71

However, before I "saved" it onto the playground, I noticed that using your code 

camera.setTarget(task[0].loadedMeshes[0].position); instead camera.setTarget(model.loadedMeshes[0].position); 

caused console errors in the inspector, so first, try that and see if it still doesn't work.

If it doesn't work, then try deleting the first camera.setTarget so we can determine that it's not because of the fact that the asset manager is asynchronous so because your scene setup is quite simple, it could be loading it very quick and changing the camera target, then being overwrite by your first camera.setTarget

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...