Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity ECS hybrid animation

Discussion in 'Entity Component System' started by creepto, Jun 5, 2022.

  1. creepto

    creepto

    Joined:
    Dec 27, 2021
    Posts:
    18
    Hey all,
    AFAIK Unity has discontinued DOTS Animation, so I'm giving up on GameObjects. I spawn one gameobject for each entity, which has a monobehavior that updates its position in LateUpdate based on entity Translation.
    Code (CSharp):
    1.         blobAssetStore = new BlobAssetStore();
    2.         _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    3.         var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, blobAssetStore);
    4.         var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(AllyPrefab, settings);
    5.         var ally = _entityManager.Instantiate(prefab);
    6.         var allyGO = Instantiate(AllyPrefabGO);
    7.         allyGO.GetComponent<FollowEntity>().EntityToFollow = ally;
    Code (CSharp):
    1. public class FollowEntity : MonoBehaviour
    2. {
    3.     public Entity EntityToFollow;
    4.     public GameObject Followed;
    5.     public float3 offset;
    6.  
    7.     private EntityManager _entityManager;
    8.  
    9.     void Start()
    10.     {
    11.         _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    12.     }
    13.  
    14.     void LateUpdate()
    15.     {
    16.         if (EntityToFollow != Entity.Null)
    17.         {
    18.             Translation entityPosition = _entityManager.GetComponentData<Translation>(EntityToFollow);
    19.             transform.position = entityPosition.Value + offset;
    20.         }
    21.     }
    22. }

    This still allows me to take advantages of burst compiled jobs because the massive data calculation (e.g. local avoidance) relies on entities and gameobjects just have to update their positions after the jobs are done.

    Yet, sometimes I need to destroy entities in parallel jobs and I store this command inside the job in entity command buffer. This creates troubles in the follower gameobject, which in LateUpdate() throws the error "The entity does not exist". Is there a safe way to destroy gameobjects before ecb destroys their relative entities?

    Also, I'm not bound to use gameobjects, it's just the best solution I can think of for animation. So if you know of any other method working for entities 0.50.1, it would be so appreciated.

    Thank you in advance!
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,217
    I can think of three options:
    1) Check EntityManager.Exists or if in a job, use StorageInfoFromEntity to see if the entity is still valid.
    2) Add an ISystemStateComponentData tag to all destroyed entities, then have a reactive system destroy associated GameObjects (note most components are removed by that point so it might not solve your problem).
    3) Wait a week, because I just got animation clips working and will be releasing my pure DOTS animation solution next weekend.