Search Unity

How to create a 0 length motion / empty animation clip

Discussion in 'Animation' started by RakNet, Nov 30, 2017.

  1. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    289
    I wanted to share my discovery on how to create an empty animation clip. This is useful in my case where I have a moddable game and I want to use a shared state machine where some states are optional. But I can see it used in other contexts. For example, it gives you a way to use AnimatorOverrideController to turn states on and off.

    If you were to go by Unity's own example:
    https://docs.unity3d.com/ScriptReference/AnimationClip.SetCurve.html

    You would expect this to work:
    AnimationClip clip = new AnimationClip();
    clip.name = clipName;
    anim.AddClip(clip, "test");

    It doesn't. Empty AnimationClips play for 1 second, they just play nothing and your animated object is frozen for that time. You can't change that 1 second from what I've tried.

    Here's a workaround:
    AnimationClip clip = new AnimationClip();
    clip.name = name;
    AnimationCurve curve = AnimationCurve.Linear(0.0F, 1.0F, .0001F, 1.0F); // Unity won't let me use 0 length, so use a very small length instead
    EditorCurveBinding binding = EditorCurveBinding.FloatCurve(string.Empty, typeof(UnityEngine.Animator), "ThisIsAnEmptyAnimationClip"); // Just dummy data
    AnimationUtility.SetEditorCurve(clip, binding, curve);
    AssetDatabase.CreateAsset(clip, "Assets/" + name + ".anim");
     
    madfish-od likes this.
  2. hk1ll3r

    hk1ll3r

    Joined:
    Sep 13, 2018
    Posts:
    71
    Awesome! Great job dude. Unity is like your typical random github project. Gotta poke around to get things working.
     
  3. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,830
    Try using float.Epsilon as the length (the smallest possible value above 0). Not that it would make any practical difference.
     
  4. hk1ll3r

    hk1ll3r

    Joined:
    Sep 13, 2018
    Posts:
    71
    I ended up creating an animation in editor with no sprite and a super small length (by setting the frames per second to 1200). Instead of the sprite, I have the animation animate a dummy variable. Gets around having to setup the animation in code.
     
    madfish-od likes this.
  5. madfish-od

    madfish-od

    Joined:
    Nov 11, 2012
    Posts:
    2
    Thanx to @RakNet

    Example of editor script below

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class DummyClipCreate : Editor
    5. {
    6.     [MenuItem("Tools/Create Dummy Clip")]
    7.     static void CreateDummyClip()
    8.     {
    9.         var path = EditorUtility.SaveFilePanelInProject("Save Dummy Animation Clip", "Dummy", "anim", "");
    10.         AnimationClip clip = new AnimationClip();
    11.         clip.name = "Dummy";
    12.         AnimationCurve curve = AnimationCurve.Linear(0.0F, 1.0F, 0.017F, 1.0F);
    13.         EditorCurveBinding binding = EditorCurveBinding.FloatCurve(string.Empty, typeof(UnityEngine.Animator), "DummyAnimationClip");
    14.         AnimationUtility.SetEditorCurve(clip, binding, curve);
    15.         AssetDatabase.CreateAsset(clip, path);
    16.     }
    17. }
     
    Last edited: Aug 27, 2020
    Miraslavk likes this.
  6. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    404
    The code works to create a short, "empty" animation, but does not actually work for the use case the OP mentions.

    The goal was to use these "empty" animations as optional states for an AnimatorOverrideController, so you can drag in the empty animation and in same cases leave them empty in the specific AnimatorOverrideController to make them "optional".

    I wanted to achieve the same thing so I found this thread, but it does not work for me. In my "base" Animator I have 3 animation states. I call them DrinkStart with a transition to DrinkLoop with a transition to DrinkEnd.

    Some of my models don't have a specific start/end animation, so I created 3 "empty" animations with the code above and used them as placeholder. In the animal AnimatorOverrideController for the animals with all animation I drag all 3 animations and it works fine, but for the animals which only have the loop animation and no specfic start / end I only drag the loop animation in the AnimatorOverrideController and so the "empty" animation plays for the start / end states.

    But this "empty" animation in the DrinkStart state causes the animal to go into a weird (T-Pose?) position before the transition to the DrinkLoop state (which has a "real" animation).

    So am I doing something wrong here or did this thread never actually achieve the OPs goal? If so is there a new / better / different way to create real "optional" states in a base animator?

    My next idea would be to create an other base Animator for animels which don't have 3 different animations per "action" but then I would still have to duplicate the logic between 2 base Animators.
     
    Last edited: Sep 29, 2021
    ludidilu22 likes this.
unityunity