Character controller plugin for Bevy.
- Movement speed (default:
400
) - Jump height (default:
2.0
) - Time in seconds to reach jump apex (top of jump) (default:
0.4
) - Time in seconds to accelerate to move speed when grounded (default:
0.1
) - Time in seconds to accelerate to move speed when airborne (default:
0.2
) - Gravity multiplier when jumpin up (default:
1.0
) - Gravity multiplier when falling down (default:
1.5
) - Time in seconds after leaving a platform to allow for a jump (default:
0.08
) - Ray casting inset (default:
1.0
) - Horizontal ray count (default:
6
) - Vertical ray count (default:
4
)
Add dependency to Cargo.toml
bevy_controller_2d = { git = "https://github.com/tqwewe/bevy_controller_2d", features = ["debug"] } # Add debug to view ray casts
Add CharacterControllerPlugin
plugin
use bevy::prelude::*;
use bevy_controller_2d::impacted::CollisionShape;
use bevy_controller_2d::{CharacterControllerBundle, CharacterControllerPlugin};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(CharacterControllerPlugin)
.add_startup_system(setup_player)
.add_startup_system(setup_ground)
.run();
}
Spawn player with CharacterControllerBundle
and CollisionShape
fn setup_player(mut commands: Commands) {
let width = 25.0;
let height = 50.0;
commands
// Sprite so we can see the player
.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::WHITE,
custom_size: Some(Vec2::new(width, height)),
..Default::default()
},
..Default::default()
})
// Collision rectangle
.insert(CollisionShape::new_rectangle(width, height))
// Character controller bundle
.insert_bundle(CharacterControllerBundle::default());
}
Spawn ground with CollisionShape
fn setup_ground(mut commands: Commands) {
let width = 100.0;
let height = 20.0;
commands
.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::DARK_GRAY,
custom_size: Some(Vec2::new(ground_width, ground_height)),
..Default::default()
},
transform: Transform::from_xyz(0.0, -ground_height, 0.0),
..Default::default()
})
.insert(CollisionShape::new_rectangle(ground_width, ground_height));
}
-
basic
cargo run --features debug --example basic
-
advanced
cargo run --features debug --example advanced
- Doesn't work with slopes
- Jump height seems to be affected with low frame rate
Thanks to Sebastian Lague, I was able to implement this following his 2D platformer tutorial series.