Skip to content

Instantly share code, notes, and snippets.

@flushpot1125
Created December 9, 2022 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flushpot1125/9c47dac7903a772e05f22c7ce469a031 to your computer and use it in GitHub Desktop.
Save flushpot1125/9c47dac7903a772e05f22c7ce469a031 to your computer and use it in GitHub Desktop.
var map ={}; //object for multiple key presses
//キー入力を受け付ける宣言
//キー入力があるたびに、map変数の値がセットされる
scene.actionManager = new BABYLON.ActionManager(scene);
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, function (evt) {
map[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
}));
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) {
map[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
}));
var distance = 0.01;
//繰り返し呼ばれる
scene.registerAfterRender(function() {
//"a"または"A"を押し続けている間、if文を実行
if((map["a"] || map["A"])){
sphere.translate(BABYLON.Axis.X, -distance, BABYLON.Space.LOCAL);
}
    //"d"または"D"を押し続けている間、if文を実行
if((map["d"] || map["D"])){
sphere.translate(BABYLON.Axis.X, distance, BABYLON.Space.LOCAL);
}
//"w"または"W"を押し続けている間、if文を実行
if((map["w"] || map["W"])){
sphere.translate(BABYLON.Axis.Z, distance, BABYLON.Space.LOCAL);
}
//"s"または"S"を押し続けている間、if文を実行
if((map["s"] || map["S"])){
sphere.translate(BABYLON.Axis.Z, -distance, BABYLON.Space.LOCAL);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment