bilberry goat
bilberry goat

Reputation: 27

KinematicCharacterController.Translation not moving object

use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
        .add_plugins(RapierDebugRenderPlugin::default())
        .add_systems(Startup, setup_graphics)
        .add_systems(Startup, setup_physics)
        .add_systems(Update, print_ball_altitude)
        .run();
}

fn setup_graphics(mut commands: Commands) {
    // Add a camera so we can see the debug-render.
    commands.spawn(Camera2dBundle::default());
}
#[derive(Component)]
enum Direction {
    Left,
    Right,
    NoMovement,
}
#[derive(Component)]
struct player_struct;
fn setup_physics(mut commands: Commands) {
    /* Create the ground. */
    commands
        .spawn(Collider::cuboid(500.0, 50.0))

        .insert(TransformBundle::from(Transform::from_xyz(0.0, -100.0, 0.0)));
  commands
       .spawn(RigidBody::KinematicPositionBased)
       .insert(Collider::ball(10.0))
       .insert(Velocity{
        linvel: Vec2::new(0.0, -10.0),
    angvel: 0.4,
       })
       .insert(Direction::NoMovement)
       .insert(KinematicCharacterController{
        offset: CharacterLength::Absolute(0.1),
        snap_to_ground: Some(CharacterLength::Relative(0.2)),
        ..default()});
}

fn print_ball_altitude(mut player_direction: Query<(&mut KinematicCharacterController, &mut Direction,)>, time: Res<Time>, keys: Res<ButtonInput<KeyCode>>, ){
  let (mut player, mut Direction) = player_direction.single_mut();
  //Let (mut player, mut Velocity) = player_velocity.single_mut();
  let speed: f32 = 300.0 * time.delta_seconds();
  match *Direction{
        // goes wrong here
        Direction::Left => player.translation = Some(Vec2::new(-speed, -4.0)),
        Direction::Right => player.translation = Some(Vec2::new(speed, -4.0)),
        Direction::NoMovement => player.translation = Some(Vec2::new(0.0, -4.0))
           ,
       }
   // check if player is pressing movement keys
        if keys.pressed(KeyCode::KeyS) || keys.pressed(KeyCode::KeyW) || keys.pressed(KeyCode::KeyA) || keys.pressed(KeyCode::KeyD)
        {
            // change the direction of player depending on what key player is pressing
        if keys.pressed(KeyCode::KeyS){
             *Direction = Direction::Down;
        }
        if keys.pressed(KeyCode::KeyW){
            *Direction = Direction::Up;
        }
        if keys.pressed(KeyCode::KeyA){
             *Direction = Direction::Left;
        }
        if keys.pressed(KeyCode::KeyD){
            *Direction = Direction::Right;
        }
    } else {
          // stop the character movement when player isnt pressing any buttons
          *Direction = Direction::NoMovement;
    }

}

im using bevy 0.13 if that helps Ive checked and I can move the body by changing it to KinematicVelocityBased and changing the velocity manually instead of using KinematicCharacterController.translation but it wont collide with objects if I do that. Also Im new to bevy and rapier so it most likely is just some very basic oversight.

Upvotes: 0

Views: 27

Answers (0)

Related Questions