@@ -54,9 +54,11 @@ pub fn setup_player(mut commands: Commands) {
54
54
// Automatically slide down on slopes smaller than 30 degrees.
55
55
min_slope_slide_angle : 30.0_f32 . to_radians ( ) ,
56
56
apply_impulse_to_dynamic_bodies : true ,
57
- snap_to_ground : None ,
57
+ snap_to_ground : Some ( CharacterLength :: Relative ( 0.5f32 ) ) ,
58
58
..default ( )
59
59
} ,
60
+ GroundedTimer :: default ( ) ,
61
+ VerticalMovement :: default ( ) ,
60
62
) )
61
63
. with_children ( |b| {
62
64
// FPS Camera
@@ -74,6 +76,26 @@ fn setup_map(mut commands: Commands) {
74
76
let ground_size = 50.0 ;
75
77
let ground_height = 0.1 ;
76
78
79
+ /*
80
+ * Sliding platform
81
+ */
82
+ commands. spawn ( (
83
+ TransformBundle :: from (
84
+ Transform :: from_xyz ( -10f32 , -ground_height, 0.0 )
85
+ . with_rotation ( Quat :: from_axis_angle ( Vec3 :: X , 40f32 . to_radians ( ) ) ) ,
86
+ ) ,
87
+ Collider :: cuboid ( 5f32 , ground_height, 5f32 ) ,
88
+ ) ) ;
89
+ /*
90
+ * Cannot climb platform
91
+ */
92
+ commands. spawn ( (
93
+ TransformBundle :: from (
94
+ Transform :: from_xyz ( 0.0 , -ground_height, 0.0 )
95
+ . with_rotation ( Quat :: from_axis_angle ( Vec3 :: X , 50f32 . to_radians ( ) ) ) ,
96
+ ) ,
97
+ Collider :: cuboid ( 5f32 , ground_height, 5f32 ) ,
98
+ ) ) ;
77
99
commands. spawn ( (
78
100
TransformBundle :: from ( Transform :: from_xyz ( 0.0 , -ground_height, 0.0 ) ) ,
79
101
Collider :: cuboid ( ground_size, ground_height, ground_size) ,
@@ -129,6 +151,11 @@ struct MovementInput(Vec3);
129
151
#[ derive( Default , Resource , Deref , DerefMut ) ]
130
152
struct LookInput ( Vec2 ) ;
131
153
154
+ #[ derive( Component , Reflect , Debug , Default ) ]
155
+ pub struct GroundedTimer ( pub f32 ) ;
156
+ #[ derive( Component , Reflect , Debug , Default ) ]
157
+ pub struct VerticalMovement ( pub f32 ) ;
158
+
132
159
fn handle_input (
133
160
keyboard : Res < ButtonInput < KeyCode > > ,
134
161
mut movement : ResMut < MovementInput > ,
@@ -169,11 +196,13 @@ fn player_movement(
169
196
& mut Transform ,
170
197
& mut KinematicCharacterController ,
171
198
Option < & KinematicCharacterControllerOutput > ,
199
+ & mut VerticalMovement ,
200
+ & mut GroundedTimer ,
172
201
) > ,
173
- mut vertical_movement : Local < f32 > ,
174
- mut grounded_timer : Local < f32 > ,
175
202
) {
176
- let Ok ( ( transform, mut controller, output) ) = player. get_single_mut ( ) else {
203
+ let Ok ( ( transform, mut controller, output, mut vertical_movement, mut grounded_timer) ) =
204
+ player. get_single_mut ( )
205
+ else {
177
206
return ;
178
207
} ;
179
208
let delta_time = time. delta_seconds ( ) ;
@@ -182,22 +211,24 @@ fn player_movement(
182
211
let jump_speed = input. y * JUMP_SPEED ;
183
212
// Clear input
184
213
* * input = Vec3 :: ZERO ;
185
- // Check physics ground check
186
- if output. map ( |o| o. grounded ) . unwrap_or ( false ) {
187
- * grounded_timer = GROUND_TIMER ;
188
- * vertical_movement = 0.0 ;
214
+ if let Some ( output) = output {
215
+ // Check physics ground check
216
+ if output. grounded {
217
+ grounded_timer. 0 = GROUND_TIMER ;
218
+ vertical_movement. 0 = 0.0 ;
219
+ }
189
220
}
190
221
// If we are grounded we can jump
191
- if * grounded_timer > 0.0 {
192
- * grounded_timer -= delta_time;
222
+ if grounded_timer. 0 > 0.0 {
223
+ grounded_timer. 0 -= delta_time;
193
224
// If we jump we clear the grounded tolerance
194
225
if jump_speed > 0.0 {
195
- * vertical_movement = jump_speed;
196
- * grounded_timer = 0.0 ;
226
+ vertical_movement. 0 = jump_speed;
227
+ grounded_timer. 0 = 0.0 ;
197
228
}
198
229
}
199
- movement. y = * vertical_movement;
200
- * vertical_movement += GRAVITY * delta_time * controller. custom_mass . unwrap_or ( 1.0 ) ;
230
+ movement. y = vertical_movement. 0 ;
231
+ vertical_movement. 0 += GRAVITY * delta_time * controller. custom_mass . unwrap_or ( 1.0 ) ;
201
232
controller. translation = Some ( transform. rotation * ( movement * delta_time) ) ;
202
233
}
203
234
0 commit comments