collider perfomance troubles

Hello,

I am working on a 2D “tower defense - like” game. So I need to display a lot of sprites (monsters) on the board. But most of all, I have a lot of colliders, because each sprite got its own collider. I’m aiming to be able to display more than 1000 monsters on the same scene.

The colliders I use are CircleCollider2D.

When I reach a certain amount of monsters, the CPU consomption is increasing dramatically. I found out that this fps collapse is due to the colliders (if I remove them, I got no fps collapse anymore).

I think that the problem is the collision between each monsters. Because I have a lot of monsters overlapping each others in a little space of the board.

I’m quite new to Unity environment, so I do not see clearly how to solve this collider performance problem. I can see a solution, but I don’t know if it’s possible in Unity: make sure the monsters ignore the collision between themselves. I need the monster’s collider to interact with different kind of projectiles and zones (AOEs), but I don’t need them to interact beetween each others. So maybe there is a way to ignore collision between themselves? So I searched for layers and 2D collisions, but I didn’t find anything. Maybe using tags or something?

Any help is welcome,
r0d.

Layers are the way to go. Put your monsters on a layer, and you can define via the collision matrix in the Physics2D settings that things on the monster layer should not collide with other things on the monster layer.

Also we don’t know if it was colliders or callback style methods like OnCollisionStay2D. So I would advocate using the profiler in addition to above advice.

1 Like

I just tried playing with the collision matrix and it’s working perfectly.
I’m still far from the perfs I’m aiming, but I have to profile it more deeply now that the main bottleneck is fixed.
Now I have to google “unity profiling”… and learn :slight_smile:

Thanks a lot.
r0d.

Hello back,

I made some quick profiling, and I don’t really understand the result. See attached image.
I don’t understand because we can see that Physics2D.Step is using nearly 50% of the CPU, but in there is no time spent in BeginContact and EndContact. So where is all this time spent?

Another thing that I dont understand: I tried to play with the Fixed Timestep value in TimeManager project options, but whatever the value i put, there is no impact on the profiling session: there is no changes.

The physics step (i assume) is what updates any objects’ physical movement and velocity, and resolves overlaps and collisions etc.

What methods are you using to move your 1000 monsters?

https://discussions.unity.com/t/676682/2

Each monster have its own collider (CircleCollider2D) and its own RigidBody2D (which is set to Kinematic).
I manage the movement of the monsters in the Update() methode (inherited from MonoBehaviour).
Then I use this.tranform to set the position and the rotation of the monster.

The collider and the RigidBody2D are component of the Monster GameObject, so I don’t need to modify them directly.

I’m trying to use the Velocity thing of the RigidBody2D, to see if I got better perfs, but at the moment it does not work (the monsters are not moving).

Kinematic rigidbodies do not get simulated using physics and velocity, you have to move them explicitly.

Try using the MovePosition function of Rigidbody2D instead of changing transform.position and see if that makes any noticeable difference.

Also you may get more insight out of the profiler if you toggle the “Deep Profile” mode at the top of that window.