Solid walls not always stopping bullets

Hi all,

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.

How can I solve this?

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.

1 Like

I made the wall thicker and the problem is less frequent now.

How would raycasts be able to solve it?

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.

Well you could adjust the timestep to compensate for velocity.

EDIT>PROJECT SETTINGS>TIME

This way, you can adjust how often the “live” computations occur.

I rely heavily on physics and have found its critical to speed the timestep up a bit for anything other then very slow moving objects.

And this,

http://wiki.unity3d.com/index.php?title=DontGoThroughThings

*some modifications here

4 Likes

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

Use the hitpoint from the raycast to spawn the bullet instantly into your enemy’s face.

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:

1 Like

If your bullet has a rigidbody attached, set Collision Detection to Continious. I know this is an old thread but maybe someone needs an answer.

6 Likes

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).

Great advice. Fixed the problem for me.

IT WORKS! Thx

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

1 Like

Perfect this was just what I needed, thanks for sharing that link.

http://wiki.unity3d.com/index.php?title=DontGoThroughThings
The code in here snaps the player back to the collided position if it was overshoot.
But how can I just stop the player at that position without the ‘snapping back’ effect?

1 Like