3
\$\begingroup\$

Its my second week with Unity. I am doing a 2D game and I have a small GameObject which should change its sprite along with following a definite path defined in Animation Curves.

I did both of them in separate .anim files since the transform animation had many keyframes, i thought it wont be good to put the '2' sprite keyframe repeatedly along side the transform keyframe.

But the problem is, I cant get it both working together at the same time. I dont want any blending because the animation is timed well already.

Also, I tried deleting the sprite change animation and tried it under script changing the SpriteRenderer.Sprite property under Update(); but it works only when the Animator component is disabled in the GameObject.

Any Solutions ? :)

\$\endgroup\$
1
\$\begingroup\$

In the Animator put the 2 animations on different layers. You might have to play with the Weight and Blending options on them a bit but it should do the trick.

The Unity3D documentation for the Animtation Layers resides at: http://docs.unity3d.com/Manual/AnimationLayers.html

\$\endgroup\$
0
\$\begingroup\$

I suppose what you mean by "change its sprite" is to change its texture. If so, have you tried something similar to this?

// Inside a co-routine after you have started playing the animation "my_sprite_anim"...
float previousTime = 0;
while (animation.IsPlaying("my_sprite_anim")) {
    // A value of 1 is the end of the animation. A value of 0.5 is the middle of the animation.
    float normalizedTime = animation["my_sprite_anim"].normalizedTime;
    // Change the texture at certain moment.
    if (previousTime < 0.5f && normalizedTime >= 0.5f) {
        Sprite mySprite = Sprite.Create(my_texture, new Rect(0, 0, my_texture.width, my_texture.height), new Vector2(0.5f, 0.5f), 100);
        my_spriteRenderer.sprite = mySprite;
    }
    // Update "previousTime"
    previousTime = normalizedTime;
    yield return previousTime;
}

So you have an animation clip responsible for translating your game object, and its sprite texture is updated via the code snippet above.

Of course, you need to make sure the references used are not null (animation, my_spriteRenderer etc.).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.