Hello, I have an issue with a fps drop and will try to explain the situation.
I have a 2D game where there is a gun/weapon that shoots multiple projectiles that spread like a shotgun. The logic of the game is that the player’s projectiles don’t collide with each other but still can collide with enemy projectiles so all projectiles are in the same layer “Projectiles”.
The moment of the shot I have a fps drop that gets bigger the more projectiles are shot at the same time and it gets lower the moment the projectiles spread out. I tested with reducing the spread to 0 and the fps drop is continuous as the projectiles fly forward bundled together. As there are different types of projectiles on some of them I need polygon collider because of their shape. On the others with box or sphere collider the issues is not visible(but it is still there). Colliders are triggers and I am deciding what to do with them(skip, destroy, explode, stuck etc) inside OnTriggerEnter2D. I really need the polygon collider because the projectiles have complex shape, they rotate in flight and then get stuck when they collide with characters.
First I tried to ignore the collision between the player projectiles with Physics2D.IgnoreCollision(col1, col2) but I still get fps drop. I checked the profiler and I have Physics2D.FindNewContacts but I no longer get OnTriggerEnter2D or OnTriggerStay2D.
Then I tried to completely disable collision between object from layer “Projectiles” in the layer collision matrix(yes the Physics2D matrix) but again get Physics2D.FindNewContacts. If i disable the colliders I no longer receive these calls.
Using Unity 2020.3.24f, tried with 2021.3.0f1 but saw no difference.
Can I get any help with what I can do to improve my logic.
If it’s because you’re seeing lots of contacts between things such as projectiles/player then using the layer collision matrix will stop that and the profiler will show you numbers of contacts etc.
I can only presume, because they’re things that move, that for the projectiles you have a Rigidbody2D on them. If not, these are Static and you’re changing the Transform which would be bad.
In the end, the profiler will show in more detail in what is taking the time. If it’s FindNewContacts then it’s likely you’re modifying the Transform causing colliders to be recreated. Recreated colliders obviously need to find any new contacts in their (instant) new position/rotation.
The projectiles have dynamic Rigidbody2D, are launched with AddForce() and added spin with AddTorque().
To make sure that my script is not doing something that may be the cause of the problem I just destroyed it after launch so the projectile is just a rigidbody with trigger collider. I removed interaction from the layer collision matrix between “Projectiles” and I still get fps drop and Physics2D.FindNewContacts in the Profiler.
I am testing with shooting spread 0 so the projectiles are flying bundled together. If I return the spread to normal like shotgun the fps is low only while they are bundled together at the beginning. So after all they are still “interacting” even though I have disabled them in the collision matrix. I double checked that they are in the correct layer.
You’d need to provide more details than “FPS drop”. It’s difficult already to just know what you’re doing without seeing some actual figures from the profiler. Even if things don’t interact, they have to be at least trivially checked to see if they are allowed to interact and this takes some time but this is a broadphase thing and is crazy fast.
In the end, it shouldn’t be difficult to duplicate this away from your main project to isolate it. Isolate these projectiles in a test project, see if you get the same amount of time spent in the profiler.
You have to understand that while I want to help, I cannot really remote debug an “fps drop”.