Unity

Estimated reading: 4 minutes 371 views Contributors

Here’s a simple worksheet to introduce learners to animation and programming triggers in Unity. It focuses on how animations are triggered through scripting, with basic examples. This will give you a hands-on approach to combining Unity’s animation system with C# scripting.


Unity Animation and Triggering Worksheet

Objective:

  • Learn how to create simple animations in Unity.
  • Understand how to trigger animations via programming.

Section 1: Introduction to Animation in Unity

1.1 What is Animation in Unity?

In Unity, animations can be created using the Animator component. You can animate objects by changing their properties over time. These properties could include movement, rotation, scale, or material changes.

  • Animation Clip: A collection of keyframes that define how an object’s properties change over time.
  • Animator Controller: Manages different animation states and transitions for an object.

1.2 Creating a Basic Animation Clip:

  1. Select a 3D object (e.g., a Cube).
  2. In the Animation window, click Create to make a new animation.
  3. Name the animation (e.g., “MoveAnimation”).
  4. Record keyframes for properties such as position or rotation to animate over time.

Section 2: Animator Controller Setup

2.1 Setting up an Animator Controller:

  1. Go to Assets > Create > Animator Controller.
  2. Name it (e.g., “CubeAnimator”).
  3. Drag the newly created Animator Controller onto your Cube.
  4. Open the Animator window by clicking Window > Animation > Animator.
  5. Drag and drop your animation clip into the Animator window.

Now, the Cube has an animation, but we need to control it through scripting.


Section 3: Triggering Animation with a Script

3.1 Understanding Animation Triggers:

You can control animations using triggers in the Animator. Triggers are a type of parameter that, when activated, make a transition between animation states.


3.2 Creating a Trigger in the Animator:

  1. Open the Animator window.
  2. Right-click in the empty space and select Create Trigger.
  3. Name the trigger (e.g., “StartAnimation”).
  4. Now, create a transition between the default state (e.g., idle animation) and your animation (e.g., “MoveAnimation”).
  5. Click on the transition arrow and set the condition to StartAnimation trigger.

Section 4: Scripting the Trigger

4.1 Writing the Script to Trigger Animation:

  1. Create a new C# script by right-clicking in the Assets folder and selecting Create > C# Script.
  2. Name the script (e.g., “AnimationTriggerScript”).
  3. Double-click the script to open it in Visual Studio (or your chosen IDE).

Replace the script contents with the following:

using UnityEngine;

public class AnimationTriggerScript : MonoBehaviour
{
    private Animator animator;

    void Start()
    {
        // Get the Animator component attached to this GameObject
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // Trigger animation on pressing the spacebar
        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("StartAnimation");
        }
    }
}

Section 5: Testing the Animation

5.1 Attach the Script:

  1. Drag the AnimationTriggerScript onto the Cube in the Hierarchy panel.
  2. Play the scene.

Section 6: Explanation and Next Steps

6.1 Explanation of the Code:

  • animator = GetComponent<Animator>();: This line grabs the Animator component from the object to control the animations.
  • animator.SetTrigger("StartAnimation");: This line triggers the “StartAnimation” trigger that was set up in the Animator.

Section 7: Additional Practice

7.1 Challenge 1: Trigger an Animation with a Different Key:

Change the key used to trigger the animation from the spacebar to another key (e.g., KeyCode.W).

7.2 Challenge 2: Add a Second Animation:

  1. Create a new animation (e.g., “IdleAnimation”).
  2. Add a new trigger (e.g., “StopAnimation”).
  3. Write a script that plays the “IdleAnimation” when pressing the R key.

Section 8: Questions

  1. What is the difference between a trigger and a boolean in the Animator?
  2. How can you use a trigger to control multiple animations in a complex animation system?
  3. What happens if you do not have a transition set up between animation states in the Animator?

This worksheet provides a simple introduction to Unity’s animation system and shows how you can use C# scripts to trigger animations based on player input.

Share this Doc

Unity

Or copy link

CONTENTS

Chat Icon Close Icon