Spot Checking Collider

I have an instant fan shaped lightning bolt arc and I would like to get a list of all GameObjects that are collided with, but I am unsure of the best way to do this.

The lightning bolt arc has a variable arc width and range, and isn’t constant shape.

The options I am aware of:
1: Do a whole bunch of raycasts (maybe 100 or so in an arc shape) and manually build a list of GameObjects that are collided with while ignoring duplicates. The problem with this is I must manually code many many raycasts. Small objects may be missed, and if I do too many raycasts it may eat up too much CPU (i.e. if I use this same arc raycast math for a flamethrower that is called every update frame).

2: Have a persistent arc shaped mesh collider. It would maintain a list of collided objects using OnTriggerEnter and OnTriggerExit. When the lightning bolt is cast it would look at this persistent list and apply damage. The problem with this is the collider is always active and using processor power, and I’ve seen funny stuff with OnTriggerExit not working when objects are destroyed or layers are changed. Also, I am unsure how to change the mesh geometry to change the arc angle.

3: Have a arc shaped mesh collider, but only activate it for 1 physics update frame when casting the spell. After the spell is cast it would need to active the collider and flag it to check for damage on the next physics update, and then deactivate it. This fixes the problem of having many colliders always active and not using them while consuming processing power. The problem with this is I must manage a spell that occurs over 2 different frames which makes the code a little more complex. Also, I am unsure how to change the mesh geometry to change the arc angle.

So in an ideal world I’d love to be able to create a mesh (or alter the shape of an existing mesh) and instantly spot check it’s collisions and get a list of all objects collided. If there is no way to do this then I think I will try option 1.

Does anybody have any tricks or alternative ideas? Thanks in advance.

As sexy as it sounds to have perfect collision, it’s often not worth the effort, and frequently has other unwanted side effects.

Often in gaming we approximate, and when things are moving rapidly and the approximation is well thought out, nobody is the wiser that you are not doing perfect collision checks.

One way to solve your problem would be with a series of sphere collision checks, or else cube collision checks, spread out in an arc across where the lightning goes. See attached graphic.

2227585--148374--approximate_colliders.png

1 Like

Ah thanks for the info. I looked at Physics.OverlapShere checks, but it looks like they don’t use the targets collider, only the targets bounding volumes according to Unity - Scripting API: Physics.OverlapSphere. Plus, as you show, the spheres don’t make a perfect arc… and it will be tricky to have variable arc widths and such.

I ended up doing an arc of capsule casts (a thin skinny capsule extruded upwards to make a plane). See photo. Something about having to program an array of capsule sweep checks like this to perform a collider spot check just seems fundamentally wrong though. Collisions are part of almost every game… there must be support for generating abnormal shaped colliders and spot checking a collision right???

2227953--148412--ArcCast.png