Skip to content

tqwewe/bevy_controller_2d

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bevy Controller 2D

Character controller plugin for Bevy.

Configuration

  • 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)

Install

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));
}

Examples

  • basic

    cargo run --features debug --example basic
  • advanced

    cargo run --features debug --example advanced

Known issues

  • Doesn't work with slopes
  • Jump height seems to be affected with low frame rate

Credits

Thanks to Sebastian Lague, I was able to implement this following his 2D platformer tutorial series.

About

Character controller plugin for Bevy.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages