Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Getting rid of ghost bullets

Discussion in 'Physics' started by Compressed, Jan 31, 2015.

?

What do you use for bullets?

  1. Raycasts

    33.3%
  2. Physical objects

    16.7%
  3. Combination of both

    50.0%
  1. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    I created a rifle which shoots bullets. Each bullet is a small rigidbody with a collider set to continuous collision detection.
    The problem is, when I launch the projectiles at a higher speed (it's not even that much super fast) they pass through colliders on walls, even thick ones.
    Now how can i fix this?
    I already halved the time step to .01 and this helped some, but the bullets still pas through smaller objects like bottles. I could lower it even more, but i don't want the performance to suffer. Can modern middle end computers even cope fine with fixed step doing like 500 frames per second?
    I definitely want to keep bullets as physical objects so I definitely don't want to use raycasting hitscans instead of real bullets.
    The only thing I can think of, is to raycast between the bullet and the place it was during the last frame, if if there is something between, to teleport the bullet back to the collision location. But this seems like overly bloatly and ugly solution.
    Thanks.
     
  2. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    This is one of the annoying things about Unity's physics engine. I'm afraid you'll probably have to use raycasts. If its a wall, use instantiate a bullet hole at the point of intersection, if it hits something else, instantiate a used bullet prefab.
     
  3. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    4,703
    Scale can also affect collisions, if your objects are scaled (not just sized) differently the collisions suffer as a result.

    The way I do it is to raycast, determine where the object would be in the next frame considering the speed that it is going, and if there's anything in between, do whatever collision stuff I want to do at the point of raycast hit.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,829
    Use raycasts. For performance, Unity uses a discrete physics system. In other words, if the bullet moves 10 meters in one physics frame, and the bad guy was 5 meters away, the bullet will jump right through the bad guy.

    You can change your physics timestep and fiddle with colliders all day long, but ultimately, the only real solution is going to be to use a raycast. It's simple, it performs well, and it's precise.