How to flip a spritesheet in Bevy

Viewed 779

I am trying to flip a sprite based on whether the player is moving left or right on the screen. My current approach of modifying the transform of the SpriteSheetComponents as follows does not seem to change the sprite at all:

  fn player_direction_system(
      velocity: &Velocity,
      _: &FaceMovementDirection,
      mut transform: Mut<Transform>,
  ) {
      let flip = velocity.horizontal.signum();
      transform.value = transform.value * Mat4::from_scale(Vec3::unit_y() * flip);
  }

Is there a different component of the sprite that I should modify in order to flip it?

1 Answers

You can absolutely work on the transform directly, but I think it would be easier to set the Rotation component instead.

fn flip_sprite_system(direction: &FaceMovementDirection, mut transform: Mut<Transform>) {
    // I'm taking liberties with your FaceMovementDirection api :)
    if direction.is_left() {
        transform.rotation = Quat::from_rotation_y(std::f32::consts::PI);
    } else {
        transform.rotation = Quat::default();
    }
}
Related