Attaching healthbar to enemies. #4564
-
Im creating a top-down, 2d shooter. I'm attempting to attach health bars to enemy players, but having a hard time understanding how I might go about it. My initial thought is to basically 'add' another sprite bundle rectangle on top of each enemy sprite bundle. Please let me know if you need to see any more of my systems and I'll add that code in. Thanks a ton in advance! fn spawn_enemy(mut commands: Commands) {
let healthbar_spritebundle = SpriteBundle {
sprite: Sprite {
color: Color::rgb(0.48, 0.98, 0.0),
custom_size: Some(Vec2::new(30.0, 3.0)),
..default()
},
transform: Transform {
translation: Vec3::new(200.0, 240.0, 0.0),
..default()
},
..default()
};
let enemy_spritebundle = SpriteBundle {
sprite: Sprite {
color: Color::rgb(0.25, 0.25, 0.25),
custom_size: Some(Vec2::new(30.0, 30.0)),
..default()
},
transform: Transform {
translation: Vec3::new(200.0, 200.0, 0.0),
..default()
},
..default()
};
commands.spawn_bundle(healthbar_spritebundle);
commands
.spawn_bundle(enemy_spritebundle)
.insert(Enemy)
.insert(Speed(150.0));
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I would use a child entity for this, you can see how they can be added here: https://github.com/bevyengine/bevy/blob/main/examples/ecs/hierarchy.rs child entites inherit their transform from the parent, which would make them follow along. |
Beta Was this translation helpful? Give feedback.
I would use a child entity for this, you can see how they can be added here: https://github.com/bevyengine/bevy/blob/main/examples/ecs/hierarchy.rs
child entites inherit their transform from the parent, which would make them follow along.