Search code examples
unity-game-enginegame-physics

Unity physics scenes copy all colliders from main scene


I am trying to use Unity's physic scenes to perform some simulations outside of the main scene. That being said, I need all of the colliders from the main scene in the physics scenes. What is the easiest way to create a physics scene with all of the existing colliders from another scene? Do I have to loop through everything, use SceneManager.MoveGameObjectToScene(...) for every object then move them back at the end of the simulation? This doesn't seem scalable.

EDIT I have a multiplayer game, when I get responses from the server I am taking their velocity and the servers position, then applying all local movement since that packet was sent via Physics2D.Simulate() to have the velocities correctly applied to the position. Now I need to do the same for the projectiles so I would need the map colliders as well as the player's. I was reading the best thing to do for physics sims is use physic scenes. If I call Physics2D.Simulate() in the main scene for more than 1 set of data (player movement vs projectiles) places I got a lot of screen jitter. Moving these simulations into separate scenes removes the jitter.

The only alternative I see is re-implementing Physics2D.Simulate() myself which seems like a bad (and complicated) idea.

Thanks.


Solution

  • I ended up only needing the collider and rigid body so I created an empty object in the physics scene with these 2 objects. Every time I needed to run the simulation, I would update the rigid body with the current players rigid body details then run the sim and apply the output velocity from the sim to the players rigid body again.