Search Unity

Recommended way to use animation with netcode

Discussion in 'DOTS Animation' started by awalltoo, Nov 21, 2020.

  1. awalltoo

    awalltoo

    Joined:
    Feb 27, 2013
    Posts:
    6
    I'm attempting to do a small project to get more familiar with how the Netcode package functions. So far I've found it to be fairly understandable, but I've hit a bit of a sticking point when trying to get any kind of animated entity into the game. I'm curious what the best way to accomplish this is. I have tried:

    - Using the DOTS animation package (com.unity.animations): This seems to be the most likely candidate in the long-term. However, as far as I'm aware, the package is in a fairly early state at the moment and still lacking in features and usability. The recommendation I've heard is not to use this package unless my goal is specifically to gain familiarity with it, which it isn't for this particular project.

    - Making a Hybrid GameObject with an Animator and setting the ConvertToEntity type to ConvertAndInjectGameObject: This works fine in non-netcode based projects. However, in order to get hybrid game objects to work, they must be instantiated from the GameObject side rather than the Entities side (i.e. Object.Instantiate(GameObject prefab) rather than EntityManager.Instantiate(Entity prefab)). Since entities replicated via Netcode are instantiated via the latter, this seems like it rules out the usage of hybrid GameObjects, unless I'm missing something.

    - Using AddHybridComponent() to create a companion GameObject with an Animator: This seems like it ought to be a reasonable way to instantiate a MonoBehaviour-based component (i.e. Animator) on an object spawned via EntityManager.Instantiate. When I attempted to do this, I successfully wound up with an Animator on my companion GameObject and I verified that my logic to trigger animations was working. However, I didn't actually see the entity animating. I haven't yet spent enough time debugging it to determine whether the problem is that the animation is only playing on the companion GameObject and not actually getting applied to the underlying entity, or whether the issue is that the companion GameObject only exists in the server world.

    In any case, nothing I've tried seems to result in a straightforward way to play animations in a game using the Netcode package. Is there a different approach that I should be taking? If not, which of the above solutions is the smoothest path to getting this working?

    Thanks for any help!
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    546
  3. awalltoo

    awalltoo

    Joined:
    Feb 27, 2013
    Posts:
    6
    Oh, that makes sense. I was aware of companion game objects from 5argon's blog, but hadn't realized that they were limited to a (relatively) small set of monobehaviours. Thanks!

    Still trying to figure out if there's an alternative to using com.unity.animation. If not, I'll dive into that and try to get myself up to speed on it as well.
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    546
    Just a correction: the link I posted isn't a complete list of supported HybridComponents, but a list of the rendering-related supported HybridComponents when using HybridRenderer.
     
  5. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    163
    I'm not using the NetCode package, but I am using GameObjects with Animators that are instantiated from ECS land. My approach is to use the Addressables package to load and instantiate the animated GameObjects and link their components to their corresponding entities in a system.
     
  6. awalltoo

    awalltoo

    Joined:
    Feb 27, 2013
    Posts:
    6
    @desertGhost_ I've considered an approach like yours - it's what I've done on other projects before companion GameObjects existed and before the ConvertToEntity flow was what it is today. It works fairly well for situations in which you don't need to get data back from MonoBehaviours into ECS (or when the data you need to copy back is limited enough that you can manually do so from a system). The reason I'm reluctant to do so in this case is that I actually want animation data to affect the ECS simulation (i.e. I want to be able to attach an object to a bone on an entity and have its ECS Translation update as the entity animates), and I don't want to have to manually write the connective logic that copies transforms from each child game object to the corresponding linked entity.

    It's a good solution for other cases though - thanks for the suggestion!
     
  7. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    163
    I handle root motion, IK (overriding bone transforms in ECS land), ragdolls, etc, but I did have to write a large amount of code to handle the connective logic to do this. I completely understand not wanting to do this.

    I'm definitely watching the DOTS Unity Animation system and will probably swap to it when it seems full featured / stable enough for my use case.
     
  8. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    I built a system on top of the Playables API and a relatively simple custom animation system in ECS. The ECS side of things runs a state machine for managing playback time of an active animation and supports basic transitions. The MonoBehaviour side of things takes that ECS state and maps it into blend weights within a Playables graph for the Animator.

    It works really well, and the plus side of using the Playables API is that time rewind, mis-predictions, etc are trivial to fix visually because you can just smoothly blend the Playables graph blend weights to the correct state.
     
    SenseEater and florianhanke like this.
  9. derpsy

    derpsy

    Joined:
    Nov 3, 2020
    Posts:
    1
unityunity