I am looking for a solution where I can instantiate a collection of objects that form a pile of objects.
The objects should be randomly generated, rotated and positioned within a box, so they form a jumbled up state.
I can just do the instantiation, and let them all fall, however I want to the player not not see them fall when the pile of objects are generated.
I have this:
private void spawnObjects()
{
Physics.autoSimulation = false;
for( int i = 0; i < spawnableObjectsPool.Count; i++ )
{
spawnObject(i);
Physics.Simulate(3);
}
Physics.autoSimulation = true;
}
Ideally that should work, but sadly does not. Clearly I am missing something. I’ve tried the putting Physics.autoSimulation call in Start, but that has other issues
I’ve tried searching for what I thought would be a common problem, but no solutions so far.
Physics.Simulate will simulate the whole scene so doing that in a loop each time you spawn an object is pretty brute force. Also, simulating the scene forward by 3 seconds isn’t the same as simulating (assuming 60hz fixed-update) 60 * 3 = 180 simulation calls each with 1/60 as the time-step. It’s likely to cause all sorts of simulation issues.
Also, you cannot just simulate your spawned objects (at least not without doing a bunch more work) but you can spawn them first and then perform the simulation until each object has “settled”. Not sure what your specific idea of settled is but you could use whether it’s sleeping or not but you’d need to be careful as it might not sleep so you could also add a limit to the number of simulation steps taken waiting for all spawned bodies to sleep. You can obviously add your own check such as seeing if it moves beyond a certain distance etc.
Unfortunately doing the above also simulates the rest of the scene so to avoid that and to make it faster would require having a separate physics scene with the geometry that these spawned objects can interact with to “settle”. Hopefully this scene has far fewer (hopefully none) dynamic bodies, only the things you spawn. You can spawn them there, simulate them to your settle points then move them to the primary scene.
You just have to be very careful with performance here.