In a game demo I am putting up for school I need to move my character using the W-A-S-D keys and also the arrow keys. I put up a function and set up a switch case to listen for any of the key presses. Here is my code snippet:
//Handles the player's 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 issue is that my character jumps so far ahead that he vanishes from the screen. Can anyone help me figure out what is wrong with my calculation?
In a game demo I am putting up for school I need to move my character using the W-A-S-D keys and also the arrow keys. I put up a function and set up a switch case to listen for any of the key presses. Here is my code snippet:
//Handles the player's 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 issue is that my character jumps so far ahead that he vanishes from the screen. Can anyone help me figure out what is wrong with my calculation?
(BABYLON.Vector3.Up() * this.movementSpeed * Tools.getDeltaTime())
is too large? Why not look at each ponent, and see why it's too large?
– Gerrat
Commented
Dec 19, 2014 at 18:02
this.movementSpeed
to 0, then obviously, your position isn't going to change at all. ...so there's relevant code you're not showing us.
– Gerrat
Commented
Dec 19, 2014 at 18:34
BABYLON.Vector3.Up()
, and Tools.getDeltaTime()`) to the console, and see which is too large.
– Gerrat
Commented
Dec 19, 2014 at 18:55
A few things -
If you want to translate using vectors (using the BABYLON.Vector3.Up() Vector) use the mesh.translate(vector, distance) function. In your case (assuming this is the right value you want to set):
this.gameObject.meshObject.translate(BABYLON.Vector3.Up(), this.movementSpeed * Tools.getDeltaTime());
I assume you did that already, but if not - turn the physics engine on and set gravity for your scene. You can learn about it in the BJS Docs : http://doc.babylonjs./page.php?p=22091
A better way to implement a jump would be to apply acceleration in the right direction (up) and letting the physics engine do its magic. Check out "Applying impulse" here - http://blogs.msdn./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
It's nearly impossible for us to help you without the rest of the code. Could you provide the entirety of your custom JS file? I'm thinking this is likely an issue with your camera angle, not so much the character movement. Also, is this a first person game or third person game?
Sorry, would have left this "answer" as a ment but I don't have 50 reputation points to do so. Trying to solicit more information to provide an actual answer.