Search Unity

physic in editor - to place stones and bake the position?

Discussion in 'Physics' started by MildakZebrak, Aug 26, 2019.

  1. MildakZebrak

    MildakZebrak

    Joined:
    Sep 21, 2017
    Posts:
    8
    Hi
    maybe a silly question but is there any way to use physics to, for example place stones in a cave?
    My idea is to have cave and set of various stones, let them fall and collide using physics so the placement look real and then disable physics and mark the stones as static objects.
    thanks
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,555
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    In theory you could create a separate scene with its own local physics and copy across the objects you want to simulate as well as things these objects collide with. You are then free to simulate the physics on this "sandboxed" scene (in edit-mode) until you are happy the physics have settled, maybe waiting for sleep perhaps. You can then copy the Transforms of the GameObject you're interested in back to the primary scene. You'll likely need a mapping of the Transforms in the new scene to the Transforms in the primary scene for this. You can then delete this duplicate scene.

    Just be aware that this'll also call physics callbacks OnCollision/OnTrigger to ensure you don't do anything that'll affect your project in a bad way or is local to the scene.

    All this could be done in an editor tool script. I've never tried this though, there may be gotchas I've not thought about. Certainly simulating the primary scene in edit-mode can be dangerous as everything is simulated and things like joint-breaking that destroys joints execute so you'd find components being destroyed so be careful with that.
     
  4. MildakZebrak

    MildakZebrak

    Joined:
    Sep 21, 2017
    Posts:
    8
    Hmm interesting.
    I'd expect some easier way to go, though. I can see many reasons to use such approach.
    GameObject Recorder seems interesting in general, thanks.
    Separate scene and copying across the object is logical, but I have no idea how to automatically copy positions/rotations for an array of objects (obviously, it is time consuming to manualy copy one by one when we've got tens of rocks in our example).
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    You can use Instantiate to clone any GameObject and its children. These clones should be put into the new scene where you can simulate them.

    Note that there's also the option of using a save playmode script (think there's something on the asset store and I've seen some examples floating around on GitHub) to persist the Transform values in play-mode.
     
    Last edited: Aug 26, 2019