Change multiple sprite in one animation clip

10,133

Solution 1

So i already figured out. All I have to do is using lateupdate function and make the "sign" object follow the "boy" object, then make simple animation in "sign" object

public Transform target;
public Transform sign;
public float xOffset;
public float zSignOffset;

void LateUpdate(){
    sign.transform.localPosition = new Vector3 (target.localPosition.x+ xOffset, transform.localPosition.y, target.localPosition.z+ zSignOffset);
}

Solution 2

You can create an animation by simply dragging a series of images to the "hierarchy" window as: boyChips0001.png boyChips0002.png boyChips0003.png

Selecting animation pictures

When you do this, Unity will automatically create a new GameObject with a "Animator Controller" and an "Animation" attached to it.

The animation created

You can delete the GameObject and the animator controller if you want, what matters is the animation, with the extension ".anim".

The animation file

You can create as many files ".anim" as you need in the same way.when you have all you need, it's time to link to your object. To do that, add a new "Animator" by pressing the "Add component" button in the Inspector and typing "Animator".

Then, create a controller Animator clicking on the "create" button on the "project" window and selecting "Animator controller"

Now, asign the animator controller to the animator.

Drag the Animator Controller to the Animator

Open the Animator window selecting the menu "window/Animator" with the Animator controller selected or by double click on its Animator Controler

Finally drag and drop your animations created to this animator window

An Animator Controller with 3 animations: "repose", "Globe1 animation" and "Globe2 Animation"

Now, your animations are ready to be used by a script. For example, the next code cycle between the animations when the user hits the space bar:

public class TransitionScript : MonoBehaviour {

    public GameObject Globe;
    string[] AnimationName;
    private int currentAnimation;
    KeyCode key;
    // Use this for initialization
    void Start () {
        currentAnimation = 0;

        AnimationName = new string[3];
        AnimationName[0] = "Repose";
        AnimationName[1] = "Globe1 Animation";
        AnimationName[2] = "Glove2 Animation";
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            print("space Down");
            Animator anim = Globe.GetComponent<Animator>();

            currentAnimation++;
            if (currentAnimation >= AnimationName.Length) currentAnimation = 0;
            anim.Play(AnimationName[currentAnimation]);

        }
    }
}

Note: The animated gameObject must have a Sprite Renderer component. If not, you can not see the animation:

Sprite Renderer

Share:
10,133
Eeva
Author by

Eeva

know php, java, c++ student

Updated on June 04, 2022

Comments

  • Eeva
    Eeva almost 2 years

    Lets say that i have two animation clip, idle and action; and two spritesheet, a boy and sign. I want boy and sign sprite animated from the same animation clip because i already made separate sprite for that. This is sign spritesheet

    sign spritesheet

    And this is boy spritesheet

    boy

    I want my animation clip consist of two animated sprite like this

    enter image description here

    I don't want make two animator (for both boy and sign) because my animator is so complicated and make two will take really long time. thanks

    ps. sorry english is not my home language :)