Search Unity

Significant Frame Drop when Multiple Rigidbody Triggers Overlap

Discussion in 'Physics' started by DiligentGear, Jan 28, 2016.

  1. DiligentGear

    DiligentGear

    Joined:
    Dec 25, 2013
    Posts:
    28
    I'm working on a bullet hell game. All bullet objects are kinematic triggers and they only collide with exactly one player object, because I don't need any physics simulation but only collision callbacks. All movements and rotations are done in FixedUpdate using rigidbody2D.MovePosition and rigidbody2D.MoveRotation instead of manipulating transform directly in Update. So far it worked really well and I could have about 1600+ bullets, which is awesome, but I just encountered the problem described in the title.

    I'm making a pattern where all bullets are centralized at one point then spreads out when it explodes, and the frame drop (dropped from 80-90fps to 40-50fps) happens when they are centralized, and back to normal when they are spread out. The profiler suggests that the performance spike is caused by Physics2D.Simulate.

    I'm trying to figure out why such frame drop happens and how I could solve it without redoing the whole collision detection by myself. Any help is appreciated.

    You can reproduce such frame drop by creating 1,000 objects with rigidbody2D and box collider2D attached at an exact same point, and attach a script that moves them at same speed. The frame drop should be pretty obvious.
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    This is an issue, tested personally. I'd workaround it by change bullets from using colliders into raycasting each update.

    Even when bullets lay on same layer and collision of that layer with self is disabled, it's still checking each bullet's collision layer with each other's, which in case of 100 colliders on top of each other is taking a 100*100 (maybe /2 if algorithm is good) checks which is an extremely huge amount.
     
  3. DiligentGear

    DiligentGear

    Joined:
    Dec 25, 2013
    Posts:
    28
    Thanks for your explanation! It makes more sense now. I was really hoping that I configured colliders and rigidbodies wrong so I don't have to workaround it. I have implemented my own collision check and it stays around 70 fps, which is good enough.