In our game we currently have different objects that don’t move and collide only with the main player. Coins, Energy capsules, etc. Up to a few hundred per scene, and all in view.
All of those have a simple CircleCollider2D, that collides only with the layer in which the Player is set.
We were wondering if it was possible to take efficiency even further, removing all the Colliders, but allowing the Objects to Register to some kind of Manager that on Update would check if the Player is in the Radius of each Object.
Since we don’t need this to be super accurate, we could run this check at 30FPS, while the rest of the game runs at 60FPS and completely independent of the Fixed Time-step.
Any ideas if all this effort would actually result in some gains or we are better off leaving it with the CircleColliders and OnTriggerEnter events? (I should have clarified: all the Colliders are set as Triggers and no Rigidbodies attached to the GOs)
Thanks!
Hundreds of static colliders in your scene should have no impact on performance because they do nothing during the simulation. The only work being done would be whatever moves or the queries you make so it’d be hard to improve on that.
That doesn’t sound too efficient because each time you’re comparing the player to hundreds of colliders. Unity physics only compares to ones that it’s nearly touching (AABB overlaps).
Thanks @MelvMay , that all makes good sense.
This is a Physics based game so the only way the Main Player feels smooth enough is by setting a lower Fixed Time-step (0.01).
I guess what I’m trying to do in some way is run the simulation for everything at a higher Time-step (the standard 0.02) and only the main player at 0.01.
Is there any way I can achieve this? I saw this other thread: 2D Physics : Manual Simulation, Simulation Groups & Multiple Worlds
And looks like that is exactly what I’m looking for, but it hasn’t made it into the main version yet.
If that’s the case, it would help with the non-static Colliders with RigidBodies (enemies, which are way less in comparison and that’s why I didn’t bring it up in the first place)
Manual simulation for 2D and 3D physics has been in for a long time now. You can turn off Auto Simulation and perform a manual simulation using Physics2D.Simulate. The smoothest motion come from running the physics simulation per-frame passing in the frame-time-delta however that reduces determinism and can cost a little more but if that isn’t so much of an issue then it’s the best option.
Thanks @MelvMay , sorry I wasn’t clear in my previous response.
We tried with Physics2D.Simulate (and it works), but there doesn’t seem to be a way for us to run, let’s say, the Manual Update for the Main Player’s Layer in every FixedUpdate, but for the Enemies in every second Fixed Update.
I saw in that other thread that there were proposals to do it by Layers (or by Tags, can’t recall). It seems like such a waste of CPU power to run the whole simulation at a 0.01 Timestep when we only need that for just 1 object.
It would be great if the simulation groups end up being implemented! (unless there’s a way to do it now that we can’t see)
Doing simulation “groups” for both 2D/3D physics is something we want to work on but it’s not something we’ve implemented yet. It’s actually a lot easier to do in 2D than in 3D but we’re trying to keep both systems in-sync as much as possible which is why it’s not been done yet. For instance, we’re currently implementing multi-scene physics i.e. multiple physics worlds per Unity scene for both systems.