- Home /
What is Physics2D.Simulate, and why is it chewing up more than 100ms of frame time? And why does this not happen when using 3D physics?
Important Edit At Bottom Of Post
If I try this with 3D colliders and physics it get ~5-10x the performance
I made a fairly simple test scenario where I had a bunch of ships all shooting at a central target. Each turret has it's own circle collider that's used to check it's range from a specific target (and to identify if a target has entered it's range). They also have a 2D line cast to make sure their target is within line of sight, and is also within a certain range.
The projectile is a small sprite with a small circle collider attached and a rigidbody.
I have 80 turrets shooting one projectile every 10 seconds (staggered) and 4 turrets shooting 10 projectiles per second. I noticed the performance tank the more turrets I added, even if none of them where actually firing.
What is this Physics2D.Simulate, and what can I do about it's horrible performance in this situation?
Additionally, Unity is only using ~11% of the performance available on the threads it's utilizing, yet the frame-rate is so low. Any comments on that?
Edit: I tried instantiating 120 projectiles per second from a single unit to see if it was the rigidbody movement, after over a thousand projectiles I was still at 90FPS. It does not seem to be that. It also does not seem to be the OnTriggerEnter or OnTriggerExit behavior either, since that is under Physics2D.ContactReporting.
Edit2: I created a part of the scene with 80 empty gameobjects and added a circle collider to each with IsTrigger selected. When the projectiles fly through the colliders the game goes from 700FPS to ~1-2FPS in waves. It will be fine for a few seocnds, then the Physics2d.Simulate spikes to a few hundred ms for a second or two, then it goes down for the next 3-4 second, then back up, then back down. I can't make much sense of it.
Screenshot of slowdown:
Screenshot of colliders:
GIF of firing:
I have no idea what the issue is, but good luck with finding it out. I hoped I helped a little.
Oh man, you really should not have deleted your answer. I had a lot of explanations under it. Thank you for your time though.
Edit: Copy/Paste of previous explanations that may not be included in the OP.
The reason I'm asking this is because I don't really have that many objects in the scene yet. 20 units with 4 turrets each for a RTS is VERY conservative. The lag actually starts up with only ~6 units and 24 total turrets. I've been program$$anonymous$$g with performance in $$anonymous$$d, but I cannot figure out how to resolve the physics lag. With a single unit I'm hovering around 3,000FPS with vsync off, and with 6 I'm down to 50FPS.
It seems a bit silly to have a RTS-like game with a 10 unit world population limit. I know there seems to be a relation between units, their colliders, and the projectiles. However I have no idea how to improve the performance, or what the root of the problem is.
I do. 20 units, 4 turrets each, 80 turrets. Essentially 80 spots where a projectile is instantiated and force is applied. (No object pooling yet, though that seems to be unrelated to this issue). Each projectile has a rigidbody2D and a circle collider 2D attached to it. They are not kinematic. Each turret has a circle collider that is essentially it's range, it gets notified when something has entered and when something has left the collider (Physics2D.ContactReporting).
Where do you think the performance hog may lie? It's not the OnTriggerEnter or OnTriggerExit methods, since both of those fall under ContactReporting. I can instantiate 120 projectiles per second till I have thousands of projectiles flying on the screen and hang around 90FPS, so it doesn't seem to be the movement of rigidbodies.
I'm at a loss
Hi, have you found any solutions to this problem? I'm having a very similar problem and have not found any solutions.
Answer by Shibli · Nov 09, 2016 at 12:44 PM
Hey, I found a potential solution. If you go into project settings-> physics you can change the collision matrix and turn all collisions off except for collisions between bullets and units. Unnecessary collision checking could be hogging cpu resources.
Your answer
Follow this Question
Related Questions
Active rigidbodies and number of Contacts 1 Answer
Box collider vs Quad(mesh) collider 1 Answer
Performance issues with trigger colliders and lots of projectiles. 2 Answers
Temporary disable collision, what's best performance-wise 1 Answer
THIN objects can fall through the ground? (AMAZING SOLUTION) 2 Answers