#how to implement projectile collision
5 messages · Page 1 of 1 (latest)
Well the first step is collision detection, like using a physics engine.
If you are using the Avian physics engine, you can label the bullet of Player A with “bulletA” and the bullet of Player B with “bulletB”. After the collision, you can detect the two colliding entities here (the order seems to be random). If one has “bulletA” and the other has “bulletB”, then destroy them.
//in update
fn detect_events(
mut started: EventReader<CollisionStarted>,
mut damage_event: EventWriter<DamageEvent>,
trap_q: Query<Entity,With<Trap>>
) {
for event in started.read() {
if let Ok(_) = trap_q.get(event.0){
damage_event.send(DamageEvent { });
return
}
if let Ok(_) = trap_q.get(event.1){
damage_event.send(DamageEvent { });
return
}
}
}```
My situation is the player takes damage when touching spikes. You can change it to check for the bullet tag. Here, it responds to any collision event.