@@ -34,7 +34,8 @@ impl Plugin for FpsControllerPlugin {
34
34
app. add_systems (
35
35
PreUpdate ,
36
36
(
37
- fps_controller_input,
37
+ fps_controller_reset_input,
38
+ fps_controller_keyboard_mouse_input,
38
39
fps_controller_gamepad_input,
39
40
fps_controller_look,
40
41
fps_controller_move,
@@ -209,7 +210,13 @@ impl Default for FpsController {
209
210
210
211
const ANGLE_EPSILON : f32 = 0.001953125 ;
211
212
212
- pub fn fps_controller_input (
213
+ pub fn fps_controller_reset_input ( mut query : Query < & mut FpsControllerInput > ) {
214
+ for mut input in query. iter_mut ( ) {
215
+ * input = default ( ) ;
216
+ }
217
+ }
218
+
219
+ pub fn fps_controller_keyboard_mouse_input (
213
220
key_input : Res < ButtonInput < KeyCode > > ,
214
221
mut mouse_events : EventReader < MouseMotion > ,
215
222
mut query : Query < ( & FpsController , & mut FpsControllerInput ) > ,
@@ -232,15 +239,15 @@ pub fn fps_controller_input(
232
239
input. yaw = input. yaw . rem_euclid ( TAU ) ;
233
240
}
234
241
235
- input. movement = Vec3 :: new (
242
+ input. movement + = Vec3 :: new (
236
243
to_axis ( & key_input, controller. key_right , controller. key_left ) ,
237
244
to_axis ( & key_input, controller. key_up , controller. key_down ) ,
238
245
to_axis ( & key_input, controller. key_forward , controller. key_back ) ,
239
246
) ;
240
- input. sprint = key_input. pressed ( controller. key_sprint ) ;
241
- input. jump = key_input. pressed ( controller. key_jump ) ;
242
- input. fly = key_input. just_pressed ( controller. key_fly ) ;
243
- input. crouch = key_input. pressed ( controller. key_crouch ) ;
247
+ input. sprint = input . sprint || key_input. pressed ( controller. key_sprint ) ;
248
+ input. jump = input . jump || key_input. pressed ( controller. key_jump ) ;
249
+ input. fly = input . fly || key_input. just_pressed ( controller. key_fly ) ;
250
+ input. crouch = input . crouch || key_input. pressed ( controller. key_crouch ) ;
244
251
}
245
252
}
246
253
@@ -280,19 +287,19 @@ pub fn fps_controller_gamepad_input(
280
287
button ( controller. pad_fly_up ) ,
281
288
button ( controller. pad_fly_down ) ,
282
289
) ;
283
- input. movement = Vec3 :: new ( move_vec. x , vertical_axis, move_vec. y ) ;
284
- input. sprint = button_input. pressed ( button ( controller. pad_sprint ) ) ;
285
- input. jump = button_input. pressed ( button ( controller. pad_jump ) ) ;
286
- input. fly = button_input. just_pressed ( button ( controller. pad_fly ) ) ;
287
- input. crouch = button_input. pressed ( button ( controller. pad_crouch ) ) ;
290
+ input. movement + = Vec3 :: new ( move_vec. x , vertical_axis, move_vec. y ) ;
291
+ input. sprint = input . sprint || button_input. pressed ( button ( controller. pad_sprint ) ) ;
292
+ input. jump = input . jump || button_input. pressed ( button ( controller. pad_jump ) ) ;
293
+ input. fly = input . fly || button_input. just_pressed ( button ( controller. pad_fly ) ) ;
294
+ input. crouch = input . crouch || button_input. pressed ( button ( controller. pad_crouch ) ) ;
288
295
}
289
296
}
290
297
}
291
298
292
299
pub fn fps_controller_look ( mut query : Query < ( & mut FpsController , & FpsControllerInput ) > ) {
293
300
for ( mut controller, input) in query. iter_mut ( ) {
294
- controller. pitch = input. pitch ;
295
- controller. yaw = input. yaw ;
301
+ controller. pitch + = input. pitch ;
302
+ controller. yaw + = input. yaw ;
296
303
}
297
304
}
298
305
@@ -335,7 +342,7 @@ pub fn fps_controller_move(
335
342
controller. fly_speed
336
343
} ;
337
344
let mut move_to_world =
338
- Mat3 :: from_euler ( EulerRot :: YXZ , input . yaw , input . pitch , 0.0 ) ;
345
+ Mat3 :: from_euler ( EulerRot :: YXZ , controller . yaw , controller . pitch , 0.0 ) ;
339
346
move_to_world. z_axis *= -1.0 ; // Forward is -Z
340
347
move_to_world. y_axis = Vec3 :: Y ; // Vertical movement aligned with world up
341
348
velocity. linvel = move_to_world * input. movement * fly_speed;
@@ -364,7 +371,7 @@ pub fn fps_controller_move(
364
371
) ;
365
372
366
373
let speeds = Vec3 :: new ( controller. side_speed , 0.0 , controller. forward_speed ) ;
367
- let mut move_to_world = Mat3 :: from_axis_angle ( Vec3 :: Y , input . yaw ) ;
374
+ let mut move_to_world = Mat3 :: from_axis_angle ( Vec3 :: Y , controller . yaw ) ;
368
375
move_to_world. z_axis *= -1.0 ; // Forward is -Z
369
376
let mut wish_direction = move_to_world * ( input. movement * speeds) ;
370
377
let mut wish_speed = wish_direction. length ( ) ;
0 commit comments