Search Unity

Best practice for applying the same kind of animation to multiple things

Discussion in 'Animation' started by paul-masri-stone, Oct 19, 2020.

  1. paul-masri-stone

    paul-masri-stone

    Joined:
    Mar 23, 2020
    Posts:
    49
    This is a question about code-reuse for animations.

    If I want to apply the same kind of animation to multiple things, is there a way to code it once and apply it to multiple things?

    Let's say that as each enemy is destroyed I show the score above them and I want to animate scale from 2 to 1 over 0.5s so that it feels punchy. So I want to apply that to all the enemy prefabs. And I also want to apply the same effect to the game score each time it changes. And hey also to damage/health each time that changes.

    Here's a tiny clip showing the sort of thing I mean.
    UnityScoreAnimationExample.gif
    Credit: Rogue Aces https://twitter.com/i/status/1313910770997121024

    And what if I change my mind later on and decide that instead I want to colour-cycle? Or spin around the y-axis? Would there be a way to make the change in one place and see it change everywhere?

    Should I do this with a state machine? Or a C# script?
     
  2. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    I think you could create an animation clip using the animation window then reference that clip? I guessing the key here is that the objects would all have to have the same property. For example if you animate the transform component's scale attribute, then want to apply the curve to another attribute like a color, I'm not sure if the curve can be remapped to another attribute. It might be possible, I just haven't tried that before.
     
  3. paul-masri-stone

    paul-masri-stone

    Joined:
    Mar 23, 2020
    Posts:
    49
    The limitation to applying the curve to the same attribute is fine by me. But how would I go about creating a standalone animation clip and then referencing it? I've only had experience creating animations on a specific game object.
     
  4. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    1. select an object that has no animator component and go into the animation window
    2. Click on the button to create an animator and clip
    3. Pick where the new objects will be saved.
    createclip.PNG

    4. The save location now has an animator controller (by default it's named after the name of the object. In my case that's "game Object" :) ) and an animation clip
    5. The animation clip has been added to the animator controller
    6. The original selected object now has an animator component and the animator controller that was created has been assigned to that component.
    test2.PNG

    In the animation window you can always create new clips (the option is under the current clips name)

    Make a clip called "default scale" Which just has the scale keyframed to the default of 1.
    I would add this to the animator (so you now have two clips)
    make transitions between the two clips.
    In the animator create a trigger param called ???? hmmmm. "Scale"

    in code call the animator's SetTrigger() anytime you want the object to play the scale anim.

    For any object that you want to have this functionality give it a animator component with this controller.


    Now, honestly, creating an animator component is probably overkill. I haven't done much in the way of handling animation data directly code side, but I'm guessing you could just make a public animationClip var and handle processing it directly at the appropriate time. I'm sure someone else here could give you some tips.
     
  5. paul-masri-stone

    paul-masri-stone

    Joined:
    Mar 23, 2020
    Posts:
    49
    @Nathanieljla It's been months since you answered – apologies for not responding sooner – I've finally had the chance to try out your suggestion and it's perfect. I had tried using a C# script but the animator is a much better way to go as it doesn't rely on recoding to make changes to the effect. Thanks!