简体   繁体   English

当一组中至少有一个射弹击中目标时,如何每组只调用一次函数?

[英]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?当霰弹枪发射一组弹丸时,假设有 1 或 20 颗弹丸击中目标:如何每次准确射击只调用一次函数,而不是每次弹丸碰撞调用一次?

When using OnCollisionEnter on the projectile or on the target itself, the method gets called for each individual pellet collision.当对射弹或目标本身使用 OnCollisionEnter 时,该方法会为每个单独的弹丸碰撞调用。

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 .OnCollisionEnter找到ContactPoint.points

Now you can use your accuracy calculation here and find out which one is perfect.现在您可以在此处使用您的准确度计算并找出哪一个是完美的。

If perfect, then call function else not.如果完美,则调用函数 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.如果有人偶然发现这一点,我的做法是为武器的每次射击生成一个唯一的 ID,并将其分配给该射击中的每个射弹。 Then any projectile that hits the target will check if that unique ID has called the method yet or not.然后任何击中目标的射弹都会检查该唯一 ID 是否已调用该方法。 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.所以这样当你发射 1 发有 20 个射弹的子弹时,每个射中目标的射弹都会对目标造成伤害,但无论击中多少射弹都只会将其击退一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq在一个语句中只调用一次函数 - Linq call a function only once in one statement 雷击中时如何仅递增/递减一 - How to increment/decrement only by one when Ray hits 如何仅使用linq按一列分组? - how to group by one column only using linq? 在 TargetFrameworks 标记中的每个框架运行一次的目标之前,如何制作只运行一次而不是一次的 MSBuild 目标? - How to make an MSBuild Target that only runs once instead of once, before Targets that run once per framework in the TargetFrameworks tag? Unity - 当弹丸击中它时,为什么我的球会朝相反的方向滚动? - Unity - Why is my ball rolling in the opposite direction when a projectile hits it? Gridview每组仅显示2个项目 - Gridview displayes only 2 Item per Group LINQ to Object - 如何为子组实现 WHERE 子句`如果至少有一个元素是` - LINQ to Object - How to implement WHERE clause `If at least one of the elements is` for sub-group 当使用.net MVC RadioButtonFor()时,如何分组以便只能进行一次选择? - When using .net MVC RadioButtonFor(), how do you group so only one selection can be made? 处理未知数量的捕获组时仅替换一个组 - Substitute only one group when dealing with an unknown number of capturing groups 正则表达式仅匹配一组 - Regex only matching one group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM