Most optimal way to animate this?

Hello,

So I was having some trouble trying to play animations in Unity. I’m wondering if there is a different way to animate game objects in Unity, or if it is possible to animate them, like the images one by one. Any help would be appericated. I am mostly looking how to animate 2D objects and I’m using C#. If some one could give an example, that would be great.

I’ve added some pics and some code to show what I am trying to do. I picked up the work after someone else had done some of the other work.

GameObject Pic

Prefab for GameObject

Prefab for target

    using UnityEngine;
    using System.Collections;
    
    public class Target : MonoBehaviour {
    
        // Use this for initialization
    
        [SerializeField]
        Animation DarkWomanSuitAni;
        Animation Splash;
    
        void Start () {
            //DarkWomanSuitAni = new Animation();
        }
    
    
    
        // Update is called once per frame
        void Update () {
            if(GameObject.FindGameObjectWithTag("lightwoman1"))
            {
                gameObject.SetActive(true);
            }
        }
    
    
    
        void OnCollisionEnter(Collision col) {
    
    
            if (col.gameObject.CompareTag ("ball")) {
    
                // SEND MESSAGE HERE
                Debug.Log("TARGET HIT: trigger animation and poin increase here");
                this.GetComponent<Animation>().Play("target_hit");
                //this.GetComponent<Player>().dunks++;
    
                //this.GetComponent<Animation>().Play("SeatFlip");
                //col.gameObject.GetComponent<Animation>().Play("LightWomanAni");
      
                DarkWomanSuitAni.Play("DarkWomanSuitAni");
                //Splash.Play("Splash_2");
              
                 //col.gameObject.GetComponent<Animation>().Play("LightWomanAni");
                 SoundManager.Instance.PlaySound("splash");
    
                // ignores collision with ball after being hit once
                // (to prevent multiple hits from the same ball)
                Physics.IgnoreCollision(this.GetComponent<Collider>(), col.collider);
            }
    }
}

Here. This uses the Animator component (mecanim), not the Animation component, but it’s a full tutorial about the process from image to spritesheet to animated GameObject. As you’ve no doubt become aware in the previous 2 threads you posted on this issue, “Animation” (the old method) is very annoying to use, has limited functionality, and is incredibly hard to debug-by-proxy, so most of the people here aren’t even trying.

Please, go through the tutorial and use the new Animator (mecanim) system, and we’ll be able to help you if you run into problems.

Alright, I noticed that this tutorial is for Unity 4.3. I’m using Unity 5.1.1. Will it work with Unity 5? Thanks for the help.

Yes, I glanced through it and it doesn’t look like there are many differences, aside from having more options in mecanim now. I couldn’t locate any sprite animation tutorials that were newer than 4.3 in the 30 seconds I took to google it, which tells me that there was likely no need for them because the current ones still worked.

If nothing else there’s almost definitely a free demo project using mecanim and sprite animation on the AssetStore. I can look for one if after completing that tutorial you feel that it wasn’t sufficient to learn what you wanted.

Ok so I got it to animate one part by following the tutorial. It is possible to add more than one animator component to a gameobject? I tried adding more, but it says you can only have one animator component to a gameobject. Is there a way to have more through code?

You can only have one animator component- an infinite number of animations can be set up and added to that component via an “Animation Controller”. He starts talking about Animation Controllers and showing how to use one about halfway through the tutorial. Keep going! :slight_smile:

Hmm, I guess I could add in the other animations into the one animator component, and then try to play them at seperate times through code. Thanks for the help and support.