Back to the main site


Quick and easy primitive animations in Unity

Posted on by Malcolm

So I was working in Unity the other day on a shooting gallery style game for my final year project only to find out that they had updated the animation system in Unity 4.3. Previously it was super easy to apply basic animations (as displayed in this now obsolete video tutorial) but I found it quite tricky to figure out how to apply a basic animation with the updated system. After about 30mins of trying to tinker with the editor I threw my hands up in the air and said “I’m a programmer, let’s sort this out using a script!”. The solution did end up using a very basic script but it also taught me something that I find very cool about animations in Unity.

Here’s the script that I used to make a game object/target “pop up” in my shooting gallery:
 
unity_anim_01
 
As you can see it’s very basic; the position depends entirely on the YPosition variable. I attached this script to the game object I would like to move and then also added an Animator component with one state/animation. The animation looks like this:
 

click for larger image

click for larger image


 
So, again a very basic example, there are two key frames with YPosition being 0 and 5 which the script uses to move the game object. And that’s it! Sure you can still can use animations by having them affect the game object transform but that can get tedious when you have many game objects (or prefabs) at different positions in 3D space each requiring a different animation because they start off at different heights (I couldn’t find a way to assign an action of motion to a prefab in the brief time I wanted to spend on this task). This method allows you to assign a motion to an object as opposed to tailoring every motion to a game object.
 
The thing I found so cool about Unity is that the animation system lets you change the variable of a script over time with all the functionality of an animation such as being able to play on request.

This entry was posted in Tutorials and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>