0

I've made a scene in Blender and now my player wont move. It doesn't have a problem loading the scene, but when I try and move with the directional keys, it won't function. Here's the code:

var BABYLON;
var canvas = document.getElementById('gamecanvas');
var engine = new BABYLON.Engine(canvas, true);
var player_height = 2;
var player_speed = 1;
var player_inertia = 0.9;

function INIT_GAME(){
    BABYLON.SceneLoader.Load('Scenes/', 'zombie_map.babylon', engine, function(newScene){
        newScene.executeWhenReady(function(){
            newScene.enablePhysics();
            newScene.setGravity(new BABYLON.Vector3(0, -10, 0));
            var light = new BABYLON.PointLight('light', new BABYLON.Vector3(0,0,10), newScene);
            var player = new BABYLON.FreeCamera('player', new BABYLON.Vector3(1,1,1), newScene);
            player.attachControl(canvas, true);
            player.ellipsoid = new BABYLON.Vector3(1, player_height, 1);
            player.checkCollisions = true;
            player.applyGravity = true;
            player.keysUp = [87];
            player.keysDown = [83];
            player.keysLeft = [65];
            player.keysRight = [68];
            player.inertia = player_inertia;
            player.speed = player_speed;
            engine.runRenderLoop(function(){
                newScene.render();
            });
        });
    });

    canvas.height = window.innerHeight;
    canvas.width = window.innerWidth;
    canvas.requestPointerLock = canvas.requestPointerLock || canvas.mozRequestPointerLock;
    canvas.requestPointerLock();

    window.addEventListener('resize', function(){
        engine.resize();
    });
}

If you can see why I can't move, let me know. Thanks!

Taylor Brown
  • 89
  • 1
  • 1
  • 9
  • 1
    Sorry, I'm not terribly familiar with this particular engine, but have you tried moving your `engine.runRenderLoop` call to the bottom of the `INIT_GAME` function, so that everything regarding your player and scene are full initialized prior to beginning the rendering of the scene? I assume the newscene parameter passed in the construction of the PointLight and FreeCamera objects are what bind them to the scene? And maybe try removing the pointer lock code temporarily to see if that lock is preventing the FreeCamera from operating. – ManoDestra Apr 01 '16 at 15:20
  • I did move it, but it didn't correct the problem. – Taylor Brown Apr 01 '16 at 15:43
  • If you need a preview, you can see the site here. Just push start. https://scrigbildefense-browntj.c9users.io/main.html – Taylor Brown Apr 01 '16 at 15:44
  • The pointer lock is the only other thing I can think of. Or that the player camera isn't being bound to the scene correctly. – ManoDestra Apr 01 '16 at 15:44
  • I also cannot look around, if that helps – Taylor Brown Apr 01 '16 at 15:46
  • Unfortunately, WebGL isn't supported on the machine I'm on at present, so it fails. All I get is a fade to white after clicking start. I did find this link, which may help (http://babylonjs-playground.azurewebsites.net/#2CEF73). In it, he seems to detachControl from the canvas before updating stuff. May be worth a look. – ManoDestra Apr 01 '16 at 15:57
  • I'll check it out and update, thanks! – Taylor Brown Apr 01 '16 at 16:00
  • In (the unlikely) case it would make a difference, I usually attach my camera to this canvas: `engine.getRenderingCanvas()`. – ConnorsFan Apr 03 '16 at 03:04
  • Also: I don't call `canvas.requestPointerLock` in my code. (Disclaimer: I don't use the SceneLoader). – ConnorsFan Apr 03 '16 at 03:21
  • I moved here my answer to your other question (http://stackoverflow.com/questions/36379819/scene-rendering-strangely-in-babylonjs/36380948?noredirect=1#comment60397085_36380948) since it seems to address the problem of camera control by the user. – ConnorsFan Apr 03 '16 at 22:29

1 Answers1

0

Your player camera should be set as the active camera:

var player = new BABYLON.FreeCamera('player', new BABYLON.Vector3(1,1,1), scene);
scene.activeCamera = player;
scene.activeCamera.attachControl(canvas, true);
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146