Search Unity

Question Simple 2D animation without animator

Discussion in 'Animation' started by Chewbacca1234, Apr 7, 2023.

  1. Chewbacca1234

    Chewbacca1234

    Joined:
    Jul 22, 2021
    Posts:
    25
    I am trying to create a simple Animation wrapper for playing a single animation. I want to avoid using unity's animator/animator controller since the only animations in my game are explosions and impact effects so I don't need any state machines and don't want to learn it right now.

    At first I created my own SpriteSheetAnimation class which didn't use any unity components, which worked but I could't figure out how to simply add a reference to a sprite with sprite mode set to 'multiple' and had to add each sprite to a List of sprites one by one.

    Then I found out about unity Animation class. I dragged and dropped the sprite sheet to the scene and created a BlueExplosion.anim file. I assigned this file to a GameObject's Animation component like this.
    https://drive.google.com/file/d/1ACJU5f1oqmCBcq8fOIsrIJbrpmtNWQw-/view?usp=sharing


    The GameObject also has a Test component which sets the animation to legacy and should start the animation when the A key is pressed.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     Animation animation;
    8.  
    9.     void Start()
    10.     {
    11.         animation = GetComponent<Animation>();
    12.         animation.clip.legacy = true;
    13.     }
    14.     void Update()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.A))
    17.         {
    18.             animation.Play(); //there is no animation visible
    19.             Debug.Log(animation.isPlaying); //only 'True' is logged to the console
    20.         }
    21.     }
    22. }
    23.  
    The problem is that each time I press 'A', there is no animation, only 'True' is printed to the console.
    Any help would be appreciated.
     
    Last edited: Apr 7, 2023