use*_*697 5 javascript babylonjs
在我准备上学的游戏演示中,我需要使用 WASD 键和箭头键移动我的角色。我设置了一个函数并设置了一个开关盒来监听任何按键。这是我的代码片段:
//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;
})();
Run Code Online (Sandbox Code Playgroud)
我的问题是我的角色跳得太远以至于他从屏幕上消失了。谁能帮我弄清楚我的计算有什么问题?
一些东西 -
如果要使用向量进行平移(使用 BABYLON.Vector3.Up() Vector),请使用 mesh.translate(vector, distance) 函数。在您的情况下(假设这是您想要设置的正确值):
this.gameObject.meshObject.translate(BABYLON.Vector3.Up(), this.movementSpeed * Tools.getDeltaTime());
Run Code Online (Sandbox Code Playgroud)我假设您已经这样做了,但如果没有 - 打开物理引擎并为您的场景设置重力。您可以在 BJS 文档中了解它:http://doc.babylonjs.com/page.php ?p=22091
实现跳跃的更好方法是在正确的方向(向上)施加加速度,并让物理引擎发挥其魔力。在这里查看“应用冲动” - http://blogs.msdn.com/b/eternalcoding/archive/2013/12/19/create-wonderful-interactive-games-for-the-web-using-webgl-and- a-物理引擎-babylon-js-amp-cannon-js.aspx
| 归档时间: |
|
| 查看次数: |
3153 次 |
| 最近记录: |