Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Spawning Prefabs with Dynamic PhysicsBodies at given a position

Discussion in 'DOTS Physics' 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.