When we want to set up some simple repetitive motions, as Coders, we may automatically start to work out a new script. But this is not always the best solution. Unity has a powerfully useful Animation system that will let us create simple or complex movements.
For this article we will be creating an Animation that makes this Satelite model swing back and forth between 15° and -15° on the Y axis.
To start, we need the Animation window ( Window>Animation>Animation ) to work in. Select the Object you wish to animate and press Create. You will be asked to select where you want to save and to name the Animation you are about to make.
Once set up, we a presented with a blank timeline like this one.
Now, We can Add the Properties we want to work with. For this example, we will add Rotation. But as you can see, we could animate changes in Position, Rotation and Scale.
When you expand the Property and gain access to the X/Y/Z. These settings are key frame that the line is Highlighting. When we start we get a single Key frame. We add a new Key frame with the 🔷symbol above the properties. IF we use Ctrl+A, we can select all key frames and using the blue line drag the animation to the time we want.
Once we have the animation set to a length we want, we can add new Key frame at the half way mark. In the Rotation.y we set the position to where we want it swing to.
By pressing the Play button in the Animation window, we can preview how the animation looks. As you can see, the timeline does the work over changing the Rotation.y value the correct amount to get it to what it needs to be for the next Key frame.
Now in the Inspector, We want to add an Animation component if it doesn’t already have one and add the animation we just created to it.
In the Animation clip (accessed in the Project view), we want to set the Wrap mode to Loop. The are a few other options to choose from, but for this one we will use Loop.
And with that we a simple animation that could of cost us dozens of lines of code and the headache of tweaking it just right.