Search Unity

Scripted animation, questions

Discussion in 'Animation' started by thelostnorthwood, Dec 23, 2018.

  1. thelostnorthwood

    thelostnorthwood

    Joined:
    May 25, 2017
    Posts:
    1
    HI

    I do 2D stuff in Unity and am a little bit past beginner in terms of Unity knowledge, and I’ve come from a background in Flash game development..

    I love Unity to death, but the thing that frustrates me is the animation system.

    I’ve just gotten back into Unity recently and am a bit rusty, but right now I’m remaking an old Gameboy game as practice, and the original game had a ton of animations.

    I have about four things at the most being animated on screen, it’s the first level of Donkey Kong ‘94 (the one with 100 levels, which I’m not going that far) and each thing on screen has its own timeline. I had to continually cycle through each object’s timeline to get timing right, but it seems so convoluted and I was frustrated so nothing ever timed right.

    Without pictures, I’ll try to illustrate it ehh - well, there’s Donkey Kong moving in from the left - he has a walk cycle, but the walk cycle is nestled under an empty game object that moves to the right (bringing DK and his walk cycle with it) - then, when he gets under the middle of the screen, his sprite is swapped (on a keyframe of walk cycle animation) so his back is to the screen, and the empty game object moves upward to simulate him climbing a ladder. So what? Well, I had to make his walk cycle as long as the cycle of the empty game object; that is, his walk cycle ends when the empty reaches the middle.

    But this is all separate timelines - two separate objects - I could have condensed the empty into the walk cycle, yes, in hindsight I should have. But I’m afraid there might be a different situation in a future game that requires an empty game object to move something along.

    Is there a better way to animate without switching between objects and their timelines to check what frame one object is in comparison to another objects’ timeline?

    Final question - in Flash, it was stupidly easy to play and stop things on the timeline through scripting - the Unity manual talks about the Animator class which has Play and Stop commands, but I can’t wrap my head around it and there are no tutorials detailing the Play/Stop commands anywhere online. Can you point me in the right direction, maybe a step by step on how to control an animation through scripting..?

    Thank you
     
  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223

    Hi!

    Just like in coding, you have variables like float, int, boolean. You can create conditions for changing animations using these variables. In the Animator tab(not the Animation tab), on the left side you'll see 'parameters' and a '+' sign.

    Code (CSharp):
    1. public class example : MonoBehaviour {
    2.     private Animator anim;
    3.     public bool inRange;
    4.  
    5.         // Update is called once per frame
    6.         void Update () {
    7.         if (inRange)
    8.         {
    9.             anim.SetBool("NAME_OF_BOOLEAN_VARIABLE_IN_ANIMATOR", true);//set its value to true or false
    10.             anim.GetBool("NAME_OF_BOOLEAN_VARIABLE_IN_ANIMATOR");//find out what its value is
    11.  
    12.             anim.SetFloat("NAME_OF_FLOAT_VARIABLE_IN_ANIMATOR", 2.0f);
    13.             anim.GetFloat("NAME_OF_FLOAT_VARIABLE_IN_ANIMATOR");
    14.         }
    15.     }
    16. }