Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

Spawning Prefabs with Dynamic PhysicsBodies at given a position

Discussion in 'Physics for ECS' started by Srokaaa, Feb 1, 2020.

  1. Srokaaa


    Sep 18, 2018
    I would like to be able to spawn a group of dynamic bodies at some runtime-determined position. It works ok for me as long as I try to spawn a single Prefab with PhysicsBody like this:

    Code (CSharp):
    2. var spawnedEntity = commandBuffer.Instantiate(entityInQueryIndex, myEntityPrefab);
    3. commandBuffer.SetComponent(entityInQueryIndex, spawnedEntity, someArbitraryTranslation);
    As soon I try to do this with a nested prefab:

    -PrefabRoot(without PhysicsBody)

    all of my entities with PhysicsBody are not translated by the translation set to their parent but are instead spawned with the translation originally set to PrefabRoot in the editor.

    This makes sense since in the source code of Unity.Physics.Hybrid/Conversion/ConversionExtensions.cs we can find:

    Code (CSharp):
    2.             manager.RemoveComponent<Parent>(entity);
    3.             manager.RemoveComponent<LocalToParent>(entity);
    4.             manager.AddOrSetComponent(entity, new Translation { Value = worldTransform.position });
    5.             manager.AddOrSetComponent(entity, new Rotation { Value = worldTransform.rotation });
    Problem with this is that it sets the translations of my bodies right when I click Play (so when conversion systems run) rather than when the prefab actually get spawned.

    Does anyone know a way around that?
  2. steveeHavok


    Mar 19, 2019
    The SpawnRandomObjectsAuthoring.cs might be useful for you.
    A number of the UnityPhysicsSamples scene extend it, to modify prefabs, instances and components at different stages.