@@ -110,3 +110,67 @@ camera.keysRight = ["D".charCodeAt(0)];
110
110
```
111
111
112
112
- [ サンプルプレイグラウンド] ( https://playground.babylonjs.com/#9T8WG5 )
113
+
114
+ ## キーボード入力でポリゴンを移動させたい {#move-by-keyboard}
115
+
116
+ ``` js
117
+ const deviceSourceManager = new DeviceSourceManager (scene .getEngine ());
118
+ let movingForward = false ;
119
+ let movingBackward = false ;
120
+ let movingLeft = false ;
121
+ let movingRight = false ;
122
+ deviceSourceManager .onDeviceConnectedObservable .add ((device ) => {
123
+ if (device .deviceType === DeviceType .Keyboard ) {
124
+ device .onInputChangedObservable .add ((event ) => {
125
+ if (event .metaKey ) {
126
+ return ;
127
+ }
128
+
129
+ if (event .type === " keydown" ) {
130
+ if (event .key === " w" ) {
131
+ movingForward = true ;
132
+ }
133
+ if (event .key === " a" ) {
134
+ movingLeft = true ;
135
+ }
136
+ if (event .key === " s" ) {
137
+ movingBackward = true ;
138
+ }
139
+ if (event .key === " d" ) {
140
+ movingRight = true ;
141
+ }
142
+ } else if (event .type === " keyup" ) {
143
+ if (event .key === " w" ) {
144
+ movingForward = false ;
145
+ }
146
+ if (event .key === " a" ) {
147
+ movingLeft = false ;
148
+ }
149
+ if (event .key === " s" ) {
150
+ movingBackward = false ;
151
+ }
152
+ if (event .key === " d" ) {
153
+ movingRight = false ;
154
+ }
155
+ }
156
+ });
157
+ }
158
+ });
159
+ scene .onBeforeRenderObservable .add (() => {
160
+ const speed = 0.01 ;
161
+ if (movingForward) {
162
+ mesh .position .z -= speed;
163
+ }
164
+ if (movingBackward) {
165
+ mesh .position .z += speed;
166
+ }
167
+ if (movingLeft) {
168
+ mesh .position .x += speed;
169
+ }
170
+ if (movingRight) {
171
+ mesh .position .x -= speed;
172
+ }
173
+ });
174
+ ```
175
+
176
+ - [ サンプルプレイグラウンド] ( https://playground.babylonjs.com/#DL4Y46 )
0 commit comments