Cheapest way to catch collisions on very fast moving objects...

Hey guys,

For our current project we need to catch collisions on objects that can be moving very quickly in opposite directions.

We're using non-kinimatice rigidbodies on both objects, one with a capsule collider and the the other with a box collider...

One object moves via translation, the other via velocity.

Most of the time this works fine, but It seems that sometimes an object can go straight through the other without colliding properly when both are at high speed.

Any suggestions on the cheapest way of solving this, is the only option to use continous dynamic collision detection on one of the rigidbodies?

Thanks in advance guys :)

Maybe you find some enlightenment on the awesome DontGoThroughThings Daniel's script. From my brief experience, the new physics improvement on Unity 3, collisionDetectionMode, still doesn't work as good as this raycasting implementation. For sure, neither is 100%, but this was pretty close on my case:

This script uses raycasting to avoid the physics engine letting fast-moving objects go through other objects (particularly meshes).

So it solves the issue of "objects moving very quickly" in the very practical instance of crossing walls that it never should be able to. You could extrapolate this script into one able to detect another object moving really fast.

The problem exists even if you use continuous dynamic collision detection because fast moving objects can move so fast that they are too far apart from itself from one frame to the next immediate frame. It's like they teleported and no collision detection would ever be triggered because no collision existed, from each frame perspective, and thus from all calculations processed.

I made an arrow with a circle collider2D(following discussion are all in 2D) at the top, and had encountered this typical issue because the circle is so small that with a fast flying speed, the arrow will fly through the edge collider without trigger any collision.

I simply solved this by changing the circle collider with a box collider, which is long enough to cope with the speed. This solved my problem well, it’s simple, and have no other overhead.

However you should keep in mind that whether this solution works is due to the length of the new collider (to be more precisely the length in the flying direction) and the speed. Higher speed requires longer length. Better test many times to decide the safe speed range in your specific case.

Go to “Edit > Project Settings > Time” and reduce the ‘Fixed Timestep’

An unorthodox solution, for linear bullet trajectories only: Resize your bullets’ hit boxes. I used this for player shots and it worked great. That was quite a few versions of Unity ago, though, so take it with a grain of salt.

The downside is if a bullet overlaps multiple enemies, there’s no guarantee which one will take the hit, the same as if the enemies were all stacked up in an overlapping pile when the bullet hit. If mandatory bullet overpenetration is not a deliberate feature of your game, you’ll need to manually sort through all collided enemies and then pick the one furthest from the visible bullet’s current location (I.E., closest to the shooter, assuming the shooter hasn’t moved since firing.) But the upside is your bullets will have exactly the same physics performance cost as if you hadn’t resized the hitboxes.

To implement: Assuming “length” is the size of the collision box parallel to the direction of the bullet, multiply the default length of your collider primitive by bullet’s speed, and use that value to set boxCollider.size. Then set to an offset that puts the front of the collider where the bullet visually is, and the back of the invisible collider sticking out behind the bullet. If the bullet travels half the screen in one update, where the bullet is now and everything the bullet passed through during the physics step will all be hit.

You want change boxCollider.size infrequently, not every update. A good time to do it is when the player collects a powerup that changes how fast their shots travel, or when they switch weapons. If the player only changes their weapons between levels, such as in a shop or upgrade menu, then you can cycle through all your pooled objects and set them then, while the player is looking at the shop UI.

Worst-case scenario, if there are frequent pickups in-game that change the speed of all the player’s bullets, or bullet speed is randomized upon firing, or the player switches weapons frequently during play, then you’ll want to scale each bullet as it is fired.

If the speed of your shots changes over time after they leave the barrel, then this answer is probably not a good solution for your use case. Likewise, if the shot curves, arcs or homes in any way, this is not a good solution. Linear bullet paths only! fortunately, most “spray and pray” weapons tend to fire dumb bullets anyway. Your spread guns, your gattling guns. Use the other solutions suggested here for things like homing missiles that probably fire one at a time anyway because they’re more likely to hit.