@@ -54,9 +54,11 @@ pub fn setup_player(mut commands: Commands) {
5454 // Automatically slide down on slopes smaller than 30 degrees.
5555 min_slope_slide_angle : 30.0_f32 . to_radians ( ) ,
5656 apply_impulse_to_dynamic_bodies : true ,
57- snap_to_ground : None ,
57+ snap_to_ground : Some ( CharacterLength :: Relative ( 0.5f32 ) ) ,
5858 ..default ( )
5959 } ,
60+ GroundedTimer :: default ( ) ,
61+ VerticalMovement :: default ( ) ,
6062 ) )
6163 . with_children ( |b| {
6264 // FPS Camera
@@ -74,6 +76,26 @@ fn setup_map(mut commands: Commands) {
7476 let ground_size = 50.0 ;
7577 let ground_height = 0.1 ;
7678
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+ ) ) ;
7799 commands. spawn ( (
78100 TransformBundle :: from ( Transform :: from_xyz ( 0.0 , -ground_height, 0.0 ) ) ,
79101 Collider :: cuboid ( ground_size, ground_height, ground_size) ,
@@ -129,6 +151,11 @@ struct MovementInput(Vec3);
129151#[ derive( Default , Resource , Deref , DerefMut ) ]
130152struct LookInput ( Vec2 ) ;
131153
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+
132159fn handle_input (
133160 keyboard : Res < ButtonInput < KeyCode > > ,
134161 mut movement : ResMut < MovementInput > ,
@@ -169,11 +196,13 @@ fn player_movement(
169196 & mut Transform ,
170197 & mut KinematicCharacterController ,
171198 Option < & KinematicCharacterControllerOutput > ,
199+ & mut VerticalMovement ,
200+ & mut GroundedTimer ,
172201 ) > ,
173- mut vertical_movement : Local < f32 > ,
174- mut grounded_timer : Local < f32 > ,
175202) {
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 {
177206 return ;
178207 } ;
179208 let delta_time = time. delta_seconds ( ) ;
@@ -182,22 +211,24 @@ fn player_movement(
182211 let jump_speed = input. y * JUMP_SPEED ;
183212 // Clear input
184213 * * 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+ }
189220 }
190221 // 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;
193224 // If we jump we clear the grounded tolerance
194225 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 ;
197228 }
198229 }
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 ) ;
201232 controller. translation = Some ( transform. rotation * ( movement * delta_time) ) ;
202233}
203234
0 commit comments