Search Unity

Hybrid RigidBodies in Editor 0.50

Discussion in 'Entity Component System' started by JooleanLogic, Apr 24, 2022.

  1. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    I have an Entity prefab (Convert and Destroy) that has a Rigidbody2D on it.
    If I drag it into the scene and press play, the Entity is created with the cloned GameObject and it falls because the built in 2D physics is acting on it. All good.

    If I instead instantiate that same prefab in code via EntityManager, the rigidBody2D is no longer affected by physics. It's like a dead component.
    Only difference between the two is the code instantiated one has a CompanionLink component on it.
    It works in a build though, just not in the Editor.

    Sleuthing around I found this UNITY_EDITOR snippet in the Clone method of CompanionLink class below. If I comment it out, it works again and as a bonus I can see the instantiated GameObjects in my hierarchy.

    I'm guessing this moves the companion GameObject to a hidden (additive?) scene such that it doesn't show up in the GameObject hierarchy and in that scene there is no physics simulation? Is that possible?

    Code (CSharp):
    1. class CompanionLink : IComponentData, IEquatable<CompanionLink>, IDisposable, ICloneable
    2. {
    3.     public GameObject Companion;
    4.  
    5.     ...
    6.  
    7.     public object Clone()
    8.     {
    9.         var cloned = new CompanionLink { Companion = UnityObject.Instantiate(Companion) };
    10. #if UNITY_EDITOR
    11.         // CompanionGameObjectUtility.MoveToCompanionScene(cloned.Companion, true);
    12. #endif
    13.         return cloned;
    14.     }
    15. }
     
    Arnold_2013 likes this.
  2. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    Did you figure this out?
    The lack of documentation around hybrid components in 0.50 is frustrating..
     
  3. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    285
    This is very interesting, I have the same issue with my audio. They only work when dragged into the scene, and in builds. But not when instantiated from code. Thanks for the CompanionLink hack, this also works for audio. :)

    Have you encountered any negative side effects with this methode?
     
  4. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Other than that hack above, no.
    I don't think Unity themselves have settled on a solution for hybrid yet and hybrid physics is especially fraught with issues.
    CopyTransformFromGameObject requires the Transform component yet since .50, you can no longer even add this during conversion.
    There's no way to opt-out of CompanionGameObjectUpdateTransformSystem.
    In my case I have parented prefabs with joints and Conversion breaks all of this. Not to mention the whole SimulationSystemGroup/FixedUpdate nightmare.

    It can work but you have to have a solid plan of attack for instantiation and post instantiation in relation to the TransformSystemGroup.

    No. Imo, hybrid is in a broken state already so I have no problem messing with it. I expect breaking changes in future versions.
     
    Arnold_2013 likes this.