in Uncategorized

Getting Started with Unity Animations

Animations are a crucial part of any unity project. In this post we explore the various method of animating game objects in unity.

Most common ways to achieve animation in unity –

  • Animation View  – This allows you to view/edit animation inside unity and apply them on game objects.
  • Animator(Mecanims) – Animator allows user to define animation as a state machine. Transition between animation can be either scheduled or done programmatically.
  • External Modeling tools – Make animations in external tools and import them as asset in unity.(I am not going to talk about this much)

Animation View

Creating animation with animation view is easy. You can create animation effortlessly wether 2d or 3d.

To create a animation just add “Animation” component on the game object, and open the animation window. By either going to windows->Animation or pressing Cmd/Ctrl +6. This will take you to the animation window like below(it might ask you to create a animation in which case just create the new file and name it).

Screen Shot 2016-03-02 at 1.49.21 PM.png

Now in the animation window, we can add the properties of the game object we want to animate. Animations works with key points these are points in time where we define the properties values.

In this example we Animate the anchored position of the Text gameobject. As you can see we have defined two key points one on 0:00 and the second on 1:00.

Playing the animation we see that the text object moves from its initial position to right till it reaches the 400 unit mark and then the animation loops again.

Animation Events

Another feature that comes with animation in unity is you can define events in animation. Events are ways to call some piece of code at certain point of time while animation is playing. To add a Event just click on the pencil icon in the side bar and specify the function to be called.

That is all you need to know to get started with animations in unity. Read more about Animators below if you want to get into advanced stuff.

Animator and Animation Controller

What if you want to have many animation on a game object for different times? Unity allows only one animation component on any game object. Well then you might attach one animation view and any time you want to play a different animation clip attach the different clip on the component. This is messy and inefficient, enter Animators.

Animator View allows you to define the set of animation in form of a state machine like so.

Add the Animator component on the game object and open the Animator View by either going to window-> Animator.

Animator will un-initialized when first created.  This is because we haven’t added a Animation Controller to the animator. This can be  going to the project window and click create-> Animation Controller. Name it something and attach it to the animator by dragging and dropping into the component.

Two boxes appear in the animator view as soon as we attach this animation controller. These boxes are AnimationStates. Unity provides two states by default. Entry is the initial state from which the state machine starts. We can create other states like Idle and Play and define animation on these states.

Now that we have some animation states. We can create transitions between these states. By right clicking on state, clicking create transition and then selecting the target state.

Once we create a transition we can decide on how to trigger this transition by clicking on the arrow and editing properties in inspector view.

Screen Shot 2016-03-02 at 2.30.24 PM.png

There properties for timing the animation and then at the bottom we have conditions. We can define parameters in animator and then use them here to trigger the transition.

For an example, i am going to create a bool parameter and then add a condition of when the bool is true(See screenShots).

This is how we control transition through parameters in Animation Controller.

There other type of parameters like int, string and trigger apart from bool. Triggers are particularly useful.

For further reading Blend Tree

Thanks for reading. Please Leave a Comment Saying hi or if something is wrong with the post.

Write a Comment