Join Adam Crespi for an in-depth discussion in this video Animating an object, part of Unity 4.3 Essential Training.
Animation is a powerful way to bring your environment to life. Beyond using the physics, we can key frame animation right here in Unity sparing us the need to make it in another program, such as 3ds Max or Maya. Now, there will be a lot of places where we'd like to animate somewhere else. There maybe more robust tools or better rigging control. But for a lot of simple animation, such as doors opening, lights turning on and fans spinning, for example, we can animate it right here. And that way we can use one model in multiple ways. Some are static and some are animated.
What I've done so far is to bring in the rest of the art, get it on those prefabs and get the right materials in. I've also brought in one of the solid walls that the art will stand on. And this way, I can start to place these walls around, put their art on, nest them underneath or parent them, and animate them. But I'll begin by looking at the animation window in animating a simple door opening. We need to make other doors operable so the player can navigate in and out of the buildings. I'll take a look at another one of the french doors, showing how to make it swing open, hold for a second or two, and then swing closed gently.
I'll open up the master building and then the gallery, and I'll scroll down to find the french doors. I'll pick French_door018 and press F to focus in. This is a french door on one of the balconies. In keeping with the other animation set to trigger earlier, I'm going to animate one of the four doors in each bay. I'll randomize which one this is, and that way the player has tension in trying to find a way out. Instead of 18, which is the same pattern as the previous animation, I'll pick 20.
This way they have to hunt around trying each one, and this will heighten the tension. I'll choose Window > Animation. In the Animation window, we start out with what looks like not a lot. We need to add in an animation curve. And we can add these in as separate objects we can call for multiple prefabs. To start out then, I can either click Add Curve and choose what to add in. Alternately, I can simply jump in and start animating, and Unity will ask me to save this as an anim file.
I'll go into the Assets folder, and into the Animations. And there's my earlier French door 11 swing anim. I'll name this one Frenchdoor20swing.anim. That way it's got its own swing, and it'll have position and rotation keyed. I'll click Save. What this does in addition to saving a separate file we can call, is allow us to stack in animations on an object. That way we can have multiple animations called in a script or sequence through them as we need. Now in the add curve, I'll scroll down and find that door.
Here's French_door020 and I'll open it up and there's the transform. In the transform, I'll pick Rotation and click on the plus next to it. Now I'm ready to animate. It starts out with a key at zero and a key at one second. Unity runs at 30 frames per second, and we can really have as much time to animate as we'd like. What I'll do is go in and delete this key at one by selecting it and right-clicking and choosing Delete Keys. Then I'll go over to 130, and I'm ready to animate.
I can either add in more keys here in the animation window or simply animate in the view. Up on top, the red play, pause and forward buttons let us know that we're animating, that we're recording what we're doing, much like auto-key in 3ds Max or Maya. I'll press E to rotate and hold Ctrl to snap this open. I'll rotate that door open. Oh, just a little past 90 and Unity makes a key automatically for me. It puts in a position and rotation key and that's okay. That's why I named this anim file specifically for this door.
Now I'll add in some more time and set it to hold for a bit, and then swing closed. I'll hold Ctrl, and scroll back in the anim window. And this way, I can add more time and see more. I'll click over at 4:30. Looking at the frames, I'm at roughly 270. It's okay to be a little off here. It'll add some character to it. Now I can either click Add a New Key, or just scrub the door back and forwards and put a key in. I'll click Add Key Frame and it puts one in in position.
The default for this is to add in a position key and while that's okay, I need a rotation key in here. What I'll do then is just hold Ctrl, click the door over and back. Later I'll come back and fix the tangents on those. Finally, I'll scroll back more, drag the time line over to 6:30 or so and close this door, again holding Ctrl so it snaps closed and animating the door closed. Now it's got an animation where the door starts out, swings open, rubber bands a bit, and swings closed.
It's good but it needs a little fine tuning. I'll pick these keys here at 1:30 and 4:30, picking one and holding Ctrl to select the other. Right-click and I've got my tangent types available. I'll pull this window up a little bit and zoom in so we can see a little better. By right-clicking, I can choose different tangents; Auto, Free Smooth, Flat, and Broken, and I've also got left and right tangent control for each. I'll choose Flat, and this way it will smooth into those keys, and they'll be flat between, having no motion.
Right now we're working in the dope sheet where we can see the key frames simply placed on the time line, and we can't actually see the curves. If we'd like to get in and adjust the curves individually, we can click on the Curves button at the bottom of the animation UI, and there we've got a traditional graph. We've got our keys available and we can get in and move them around as we need. If we zoom in far enough we'll be able to see their handles and selecting the keys shows us those Bezier handles. We can custom tune that animation to whatever we need. For now though, flat tangents will work nicely and I'll close this animation window.
My door is ready. Now what I've got on here is an animation. I'll add an animation controller onto the door by scrolling down, and choosing Add Component > Miscellaneous > Animation. There's Animation and Animator. Animator goes into Mecanim for behaviors and trees and states. Animation is far simpler and usable for things like this. I'll pick Animation and then choose that animation. I'll uncheck Play Automatically and I'm ready to tie this into a trigger collider, letting the door open when the player gets close enough.
- Designing the game
- Creating and transforming objects
- Importing and configuring models and textures
- Setting properties in the Inspector
- Creating the terrain geometry
- Building materials and adding shaders
- Creating GameObjects
- Exploring physics
- Animating objects
- Lighting the scene
- Creating 2D game elements
- Adding special effects