简体   繁体   中英

When at least one projectile out of a group hits the target, how to call a function only once per group?

When a shotgun fires a group of pellets, and let's say 1 or 20 pellets hit the target: how do I call a function only once per accurate shot, instead of once per pellet collision?

When using OnCollisionEnter on the projectile or on the target itself, the method gets called for each individual pellet collision.

Should I use an event for this problem? How would I do this?

Edit: Ah, I forgot to edit this once I figured it out. I think some people might've misunderstood my problem.

Inside OnCollisionEnter find ContactPoint.points .

Now you can use your accuracy calculation here and find out which one is perfect.

If perfect, then call function else not.

If anyone ever stumbles upon this, the way I've done it is by generating a unique ID for each shot of a weapon, and assigning it to each projectile inside that shot. Then any projectile that hits the target will check if that unique ID has called the method yet or not. So this way when you fire 1 shot which has 20 projectiles, each projectile that hit the target will do damage to the target, but only knock it back once, no matter how many projectiles hit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM