Search Unity

Question Can I programmatically create the animations in unity? (Not at runtime, In Editor)

Discussion in 'Animation' started by LOKOM3, Oct 27, 2021.

  1. LOKOM3

    LOKOM3

    Joined:
    Jun 17, 2021
    Posts:
    6
    Hello, I am currently working on a 2D game, I am making my animations using sprites, not a spritesheet, each image separately.

    Each animation is 6 frames long and did not animate any other parameter other than the spriterenderer

    What I am looking for is to make several enemies that have the same behavior, Idle <-> Move -> Die, so I want to make a script that when I execute them creates the animation for each of my enemies (I have a database of my enemies) looking in the folder of each enemies where I put the sprites and generate the sequential animation, to save time since I feel that it is too long.

    Plus, if it is possible to add an event and set the method to it.

    Example of my animation
    cap_uni_01.PNG

    States of each enemy:
    cap_uni_02.PNG

    PD: To use them I have a Prefab of each enemy. To emphasize that what I am looking for is not in runtime, it is from the editor
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    I'm sure this is possible with some moderately advanced coding knowledge, but a much easier way of achieving this is to simply re-use the same animations for all of your enemies.

    Animations in unity are name dependent for children; however, the game object containing the Animator component can be any name you like. You can attach the same animator controller to any number of enemies, and the animations should work fine.

    If anything is unclear, please let me know.
     
  3. LOKOM3

    LOKOM3

    Joined:
    Jun 17, 2021
    Posts:
    6
    Thanks for your reply.

    But I'm not sure how it works, if I have my gameobject and inside the animation the sprite of the SpritereRenderer is changed, although I reuse the same controller for multiple objects this will use the sprite say of my enemy 1 on all objects?
     
  4. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    So I realize the way I explained things was confusing and I also left out a lot of important information.

    If you want to swap out different sprites for each enemy, then unfortunately I misunderstood the question; I don't know how to do that.

    If you want to animate the position/rotation, or anime the same sprites for each enemy, then instead of writing an essay to try and explain further, I recorded a few videos demonstrating the process.

    ---

    Shared Animator for swapping sprites in the Sprite Renderer:
    https://drive.google.com/file/d/1V66I5RV4eitRoT-UQVVFE8IWLqWn8xyd/view?usp=sharing

    Shared Animator for animating position/rotation:
    https://drive.google.com/file/d/19wEYYf4n9F9huerUhL9mQsBMaiDNs1Ll/view?usp=sharing

    ---

    For animating position/rotation, you need to have the sprite be a child object, and all of the children across your different enemies need to have the same name (in the example video, it was EnemySprite). If you animate the position of the parent, you'll be animating the global position, which would cause every enemy to snap to that exact spot when the animation is played.

    If anything is still unclear, please let me know.
     
    Last edited: Nov 1, 2021
  5. LOKOM3

    LOKOM3

    Joined:
    Jun 17, 2021
    Posts:
    6
    Oh, just this is what I need. I really thanks for your help.
    From what little I know unity I think it is not possible, that's why I thought more about a solution to generate the animations in Bacth, using programming
     
  6. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Yeah, sorry about that. It would be possible with programming, but it's beyond what I currently know. You could do something manual, like replacing the sprites via code instead of using the animator, but that's probably more of a pain than it would be worth.
     
  7. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Have a look at our feature Sprite Swap, it feels like this feature does exactly what you are looking for.
     
    Unrighteouss likes this.
  8. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    I got the impression he was using frame-by-frame animation, but looking at the opening question, I realize that may have been a mistake. " I am making my animations using sprites, not a spritesheet" suggests something like a 2D rig, but I was thrown off by "Each animation is 6 frames long and did not animate any other parameter other than the spriterenderer" which sounds like frame-by-frame.

    If he's animating the Sprite Renderer, I'm not sure Sprite Swap would help, but using Sprite Swap and animating transforms does sound like a better solution.
     
    Ted_Wikman likes this.
  9. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Yeah, not 100% sure what @LOKOM3 is trying to create.
    I saw this section
    and thought of our Flipbook Animation Swap example, where we have different characters using the same animation clip to animate different sequences of Sprites.
     
    Unrighteouss likes this.