Creating Simple Animations (Unity Tutorial)

4 min read 1 year ago
Published on Aug 03, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to create simple animations in Unity by animating collectibles in your game scene. We will focus on making diamonds spin and move up and down, enhancing their visual appeal to attract player attention. This guide assumes you have a basic understanding of Unity and have a project set up.

Chapter 1: Creating an Animation

  1. Select the Diamond

    • Open Unity and navigate to the scene with your collectibles.
    • Select one of the diamonds from the hierarchy.
  2. Open the Animation Window

    • Go to the menu bar and select Window > Animation > Animation.
    • This will open the Animation window.
  3. Create a New Animation Clip

    • Click the Create button in the Animation window.
    • Navigate to the Animations folder and name the new animation clip Diamond Idle.
  4. Add Properties to Animate

    • Click Add Property in the Animation window.
    • Expand the child Gem object and select Rotation.
    • Keyframes will be automatically created at the start and end of the animation.
  5. Set Keyframe Values

    • At the start, the rotation should be set to 0.
    • Move to the next keyframe and set the Y value to 360 to complete a full rotation.
  6. Adjust Animation Duration

    • Zoom out using the mouse wheel until you can see the 10-second marker.
    • Drag the last keyframe to the 10-second mark to slow down the rotation speed.

Chapter 2: Exploring Animation Curves

  1. Understand Animation Behavior

    • Press play to observe the diamond’s rotation. Notice it slows down and stops at the end of the animation.
  2. Switch to Curves Tab

    • In the Animation window, switch to the Curves tab to view the animation curve.
  3. Change Curve to Linear

    • Select the Y value on the left panel.
    • Right-click the first keyframe and choose Both Tangents > Linear.
    • Repeat for the last keyframe to ensure constant rotation speed.
  4. Test the Animation

    • Play the animation again to verify that the diamond spins at a constant rate.

Chapter 3: Recording New Keyframes

  1. Add Position Animation

    • Switch back to the Dope Sheet view in the Animation window.
    • Click Add Property and select Position of the gem.
  2. Set Initial and Final Positions

    • Keep the starting and ending keyframes at the original position.
  3. Insert Keyframes for Movement

    • Move to approximately 2.5 seconds on the timeline and press the Record button.
    • Set the Y position to 0.5; a new keyframe will be created.
    • Add another keyframe at around 5 seconds with a Y position of 0.4.
    • Finally, add a keyframe at 7.5 seconds with a Y position of 0.5.
  4. Turn Off Record Mode

    • Disable record mode and play the animation to observe the up-and-down movement.

Chapter 4: Applying the Animation to a Prefab

  1. Close the Animation Window

    • Once the animation is complete, close the Animation window.
  2. Check the Animator Component

    • Click on the diamond in the hierarchy to see that it has an Animator component assigned.
  3. Confirm Animation State

    • Double-click the Animator component to confirm the entry state is set to the Diamond Idle animation.
  4. Apply Changes to All Diamonds

    • With the diamond selected, click on Overrides and choose Apply All to apply the animation to all diamond prefabs in the scene.

Conclusion

Congratulations! You have successfully created a simple animation for collectibles in Unity. The diamonds now spin and move up and down, enhancing their visibility in the game. As a next step, consider experimenting with different animations or adding more collectibles to your scene. If you have any questions or need further assistance, feel free to leave comments or check out additional resources. Happy animating!