0

In my game I want to move my character using WASD keys. I have set up a class with a method that updates when the player presses any of the WASD keys to move the character. The problem is that it is not updating the character's movement every frame. Every time I press the W key for instance and let it go the character "teleports". Here is the class I made:

//character speed
var movementSpeed;
//character rotation
var rotationSpeed;

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

    PlayerMovement.prototype.onKeyReleased = function (key) {
        switch(key)
        {
            case KeyType.W:
            case KeyType.UpArrow:
                console.log("Moving up");
                this.gameObject.meshObject.position.z += movementSpeed * Tools.getDeltaTime;
                break;
        }
    }

    return PlayerMovement;
})();

My question is: what am I doing wrong?

user3838697
  • 115
  • 3
  • 12

1 Answers1

0

You are using onKeyReleased for the event. That means it will not calculate the position until you actually release the key. You probably want onKeyDown

Chris Franklin
  • 3,864
  • 2
  • 16
  • 19
  • Oops, I made that change. Thank you. But my character doesn't move when I press the key though. Any suggestions? – user3838697 Dec 18 '14 at 23:19
  • Your movement speed is still `movement = 0` in the code above. `0 * timeDelta` is still 0. – Chris Franklin Dec 18 '14 at 23:21
  • Oh, I am actually setting the value in another class – user3838697 Dec 18 '14 at 23:23
  • Print out the value to your console if you would. It seems like you are declaring two different scopes of movement speed. I suspect (simply based on the code above) that you are not operating on the variable you think you are. If you are seeing the movement event printing out in the console, but the character is not moving that is the first thing I would examine. – Chris Franklin Dec 18 '14 at 23:31
  • Well, when I had the onkeyrelease callback the character was moving (more like skipping) so I believe that the movement speed is fine. Because I have even changed the movement speed variable with an actual number (5 or even 10) and my character still stays still. – user3838697 Dec 19 '14 at 14:40
  • Ok, but all I can go on is what you have provided. Just telling me 'It isn't working' is not helping me pinpoint what else the problem is. I am not familiar with Babylon.js enough to know the inner workings of it's key handling so I need more information from you to help you pinpoint the problem. I am still unclear if the position is being updated with `onKeyDown()`. Can you add `console.log( this.gameObject.meshObject.position );` to the last line of the function and report the results. I need to know how often it fires and what values it gives to help you. – Chris Franklin Dec 19 '14 at 18:32
  • I did and the result is Vector3 {x: 12.15096, y: 0, z: NaN, toString: function, asArray: function…} – user3838697 Dec 19 '14 at 18:37
  • Ok, as I said earlier `this.gameObject.meshObject.position.z += movementSpeed * Tools.getDeltaTime;` is wrong then. If you are getting Z as `NaN` that means one of those values is not set properly. So, the answer is correct, you just need to figure out how to pass the proper values. Please accept the answer and open another question if you still can't figure this out. – Chris Franklin Dec 19 '14 at 19:57