[2D] Change sprite color for a fixed period of time #5288
-
I have an 'enemy' entity that receives damage. Every time the enemy receives damage, I want it to flash red for a brief period of time. How might I achieve this? My code: // remove health from the enemy and destroys the enemy (death) when all health is depleted
fn receive_damage(
mut commands: Commands,
projectile: Query<(Entity, &Transform, &Damage), With<Arrow>>,
mut enemy: Query<(
Entity,
&Transform,
&Sprite,
&mut Health,
With<Enemy>,
Without<Arrow>,
)>,
) {
if let Some((projectile, projectile_pos, damage)) = projectile.iter().next() {
for (enemy, enemy_pos, sprite, mut health, _, _) in enemy.iter_mut() {
if enemy_pos.translation.distance(projectile_pos.translation)
< sprite.custom_size.unwrap().x / 2.0
{
// despawn projectile when contact with enemy is made
commands.entity(projectile).despawn();
health.0 -= damage.0;
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can use a Timer. Check out this example. I would suggest perhaps creating a 'Hurting' component that holds the timer, and applying the component to the enemy when it needs to be coloured red. After the timer runs out, you can remove both the component and the red tint. |
Beta Was this translation helpful? Give feedback.
-
Thanks Jazarro! This is exactly what I was looking for. |
Beta Was this translation helpful? Give feedback.
You can use a Timer. Check out this example.
I would suggest perhaps creating a 'Hurting' component that holds the timer, and applying the component to the enemy when it needs to be coloured red. After the timer runs out, you can remove both the component and the red tint.