Unity Animator

I dabbled with Unity’s animator a while back but ever posted about it. Having revisited the topic today I’ll cover both parts of the exercise in one post. Initially I had heard that the animator could be used to control the doors in my walking simulator but at that time never looked into it and instead relied on using the iTween plug-in. Having learnt what I have today I’ll rely on the Animator from now on, it uses almost no code and is far simpler to use. Especially for the basic movement of doors, windows etc. We setup a swinging lamp and an opening door, the lamp would swing forever on a loop but the door would be triggered by script to call upon its various states of animation.

Swinging Lamp

I loaded up the old room scene we were given at the start of the year (as an introduction to the walking sim exercise) and created a lamp shape out of primitives. The issue with this is that all Unity primitives have their pivot point set to the centre, and this isn’t useful for a swinging lamp. There are two ways around this, you either model the object in another package and set the pivot point before import or parent all the Unity primitives to an empty game object and use that as a pivot.

Capture2Capture

Once you get into the Animator (Ctrl+6) it’s a familiar sight if you’ve used any other animation software. The keyframes are similar to those in After Effects. Unity runs at 60fps default so there is a little more space to fill every second but for the sake of swinging a lamp it wasn’t an issue. Move to one extreme and key, move to the other and key and then smooth the curves out in the curve sheet so the loop works better. Jump back into gameplay and the lamp will swing forever in the scene. I can think of very few scenarios where an object in a scene would have perpetual motion, however we’ll address this in the next exercise.

lampswinggif

Door Opening

As I mentioned earlier, knowing about animation states would have been infinitely useful for my walking sim. The best way I can explain it is linking together small clips of animation using transitions.  Here is a diagram that will make far more sense than me:

door_animator

So during the exercise I created three animation states, door idle, door opening and door closing. The arrow flow in the diagram are your transitions, for example door idle is your default state of the door, closed in its frame with no animation. Idle can only realistically transition to door open, transitioning to door close would require the door to be open first. Oh I should also mention that the timings of the door will all be dependent on your key frames like any animation.

Once we have setup code, an object/button in the scene will allow us to move through these transitions with ease. Even if the door is only half open and we click to transition, Unity will automatically figure out how to smoothly close the door without jumping to the beginning of the door close animation. Absolutely genius stuff and would have saved me days of headaches back in November.

However I should note setting up the transitions is not the end of the story, if you play the scene the door will loop forever even if the code was in place. First we need to untick the Has Exit Time radio button and add a trigger condition to every transition, in this instance it was named toggle.

door_inspector_dooridle

Within the inspector properties for each individual animation cycle the radio button for ‘Loop Time’ also had to be disabled.

door_script

Add this wonderfully simple bit of code to an object in the scene, replace the triggerName in the inspector with ‘Toggle’ and magically a working door happens with almost no scripting. We could have set the triggerName in stone but its nice to be able to re-use the script elsewhere.

dooropengif

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s