4

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?

EtTuBrute
  • 502
  • 5
  • 16

1 Answers1

6

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();
    }
}
cart
  • 286
  • 1
  • 3
  • You should also make sure you're using the latest version of Bevy. We only recently added support for this. – cart Aug 23 '20 at 23:59
  • With the [transform system rework](https://bevyengine.org/news/bevy-0-2/) from bevy v0.2, that would be: `transform.set_rotation(Quat::from_rotation_y(std::f32::consts::PI));` (using `bevy::math::Quat`). – Paul Götze Oct 02 '20 at 20:18
  • With bevy v0.3 that would be `transform.rotation = Quat::from_rotation_y(std::f32::consts::PI);`. – Frostack Nov 04 '20 at 18:09