I have a player character that shoots a shotgun. I have placed a wall to test collisions. The wall has a box collider that is the same size as the wall. When I shoot the shotgun at close range, the wall will stop all bullets and they drop to the ground. However, if the range is larger, the wall will only stop part of the bullets and some bullets hit something behind the wall.
Hi!
This most probably has to do with having the bullets flying so fast, that the physics engine wont be able to figure out that they are actually colliding.
You could verify this by making the wall colliders thicker, or the bullets go slower. If this makes the bullets collide more reliably, this is your problem.
As for fixing it, there are several options, raycasting might be the simplest.
A raycast literally casts a ray forward untill it finds the first object in it’s trajectory, like a laser.
physics are generally “live” computations every few moments, which check if a physics condition is currently happening, a high velocity bullet could potentially be way past the wall before it’s checked by physics wether it collided already.
I went on the journey of trying all these different methods and I realized something. I’m doing a 2D game and I had a variation of bullet direction for the sake of making AI miss their target. I needed to change the variation to z,y and have no x variation because the x variation meant that the bullets would shoot above and below the level colliders.
Perhaps the other methods did work but because I had bullets missing the colliders due to the x variation, once I fixed that, the following code was sufficient:
Continuous Collision Detection generally won’t be enough for really fast objects that are also fairly small. In my game, the player fires a fast-moving projectile, which is only about the size of a baseball. Essentially, in one frame it moves further than its own diameter. This can result in it not detecting collision with something like a wall, as the physics system moves it from one side of the wall to the other, never actually overlapping the wall.
The solution, in my case, was to perform a raycast each frame, before moving the object forward, instead of using normal physics forces. When the raycast hits something, you get the hit information (target and hit point).
Couldn’t it also help to make the bullet’s collider longer? As in you make the collider go from the nose of the bullet but further beyond it’s tail. And maybe also make it so that if the bullet is touching the target AND the wall at the same time, it doesn’t deal damage to the target, since that would mean it was supposed to be stopped by the wall