Character movement with Babylon.js

In the game demo I am placed in a school, I need to move my character using the WASD keys as well as the arrow keys. I set up a function and set a toggle case to listen for any key press. Here is my code snippet:

//Handles the player movement
var PlayerMovement = (function () {
    //Constructor
    function PlayerMovement() {
        this.gameObject = null;
        this.movementSpeed = 0;
        this.rotationSpeed = 0;
    }

    PlayerMovement.prototype.awake = function () {
        console.log("Awake");
    };

    PlayerMovement.prototype.update = function () {
        //console.log(Tools.getFps());
    }

PlayerMovement.prototype.onKeyPressed = function (key) {
        switch(key)
        {
            case KeyType.W:
            case KeyType.UpArrow:
                console.log("Moving up");
                this.gameObject.meshObject.position.z += (BABYLON.Vector3.Up() * this.movementSpeed * Tools.getDeltaTime());
                break;
            case KeyType.A:
            case KeyType.LeftArrow:
                //TODO: Do stuff
                break;
            case KeyType.S:
            case KeyType.DownArrow:
                //TODO: Do stuff
                break;
            case KeyType.D:
            case KeyType.RightArrow:
                //TODO: Do stuff
                break;
        }
    }
 return PlayerMovement;
})();

      

My problem is my character jumps so far forward that it disappears from the screen. Can anyone help me figure out what is wrong with my calculations?

+3


source to share


2 answers


A few things -



  • BABYLON.Vector3.Up () - (0,1,0). Multiplying this object by any number will return NaN. I think the object doesn't bounce off the screen, it just disappears.
  • Z is not up :-) position.y should be changed if you want to jump.
  • If you want to translate with vectors (using the vector BABYLON.Vector3.Up ()) use the mesh.translate (vector, distance) function. In your case (assuming this is the correct value you want to set):

    this.gameObject.meshObject.translate(BABYLON.Vector3.Up(), this.movementSpeed * Tools.getDeltaTime());
    
          

  • I assume you have done this already, but if not, turn on the physics engine and set gravity for your scene. You can read about this in the BJS Docs: http://doc.babylonjs.com/page.php?p=22091

  • The best way to do a jump is to accelerate in the right direction (up) and let the physics engine do its magic. Have a look here "Apply Impulse" - http://blogs.msdn.com/b/eternalcoding/archive/2013/12/19/create-wonderful-interactive-games-for-the-web-using-webgl-and-a -physics-engine-babylon-js-amp-cannon-js.aspx

+3


source


It's almost impossible for us to help you without the rest of the code. Could you please provide your entire JS file? I think this is probably a problem with your camera's angle of view and not so much with the movement of the character. Also, is it a first person game or a third person game?



Sorry to leave this "answer" as a comment, but I don't have 50 reputation points for this. Trying to ask for more information to get an actual answer.

0


source







All Articles