0% found this document useful (0 votes)
64 views

Unity Tutorial - How To Use Animator Controllers and Triggers - Studica Blog

This document provides instructions for creating a simple animation controller in Unity to change the color of a 3D cube based on user input. It explains what animation controllers are and how they allow games to play different animations based on conditions like player movement inputs. It then provides a step-by-step tutorial to create an animation controller with two animations - one that makes the cube red when the spacebar is pressed, and one that returns it to white when spacebar is pressed again.

Uploaded by

Jesse Kazemek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Unity Tutorial - How To Use Animator Controllers and Triggers - Studica Blog

This document provides instructions for creating a simple animation controller in Unity to change the color of a 3D cube based on user input. It explains what animation controllers are and how they allow games to play different animations based on conditions like player movement inputs. It then provides a step-by-step tutorial to create an animation controller with two animations - one that makes the cube red when the spacebar is pressed, and one that returns it to white when spacebar is pressed again.

Uploaded by

Jesse Kazemek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

The Source for Education Technology Products

    
(HTTPS://WWW.STUDICA.COM/BLOG/
(https://www.facebook.com/studica)
(https://twitter.com/studica)
(https://www.linkedin.com/company/studica)
(https://www.youtube.com/user/StudicaNews)
(https://instagram.com/studicacom/)
(https://www.pinterest.com/studica

Education & Technology Blog


( HT T P S :/ /W W W. ST UDI CA .CO M/ B LO G/ ) EDUCATI ON & TECHNO LO GY B LOG (HT TPS :/ / WWW.S T UD ICA. CO M/BLOG)
U N I T Y T UT O RI AL: HO W T O U SE A N IM AT O R C ONT RO LLERS AND TRIG GERS (HT TP S:// W WW.S TU DI C A.CO M/ B LO G/UN ITY-
T UT ORI AL -ANIMAT OR-C ONT RO LL ERS )

Unity Tutorial: How to Use Animator Controllers and Triggers


kristiel / December 28, 2016 /
Tutorial (https://www.studica.com/blog/category/tutorial),
Unity Game Development (https://www.studica.com/blog/category/unity-game-development-platform)

SHARE THIS POST

https://www.studica.com/blog/unity-tutorial-animator-controllers 1/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/
(mailto:?
subject=Unity+Tutorial%3A
thought
you
might
enjoy
this!
Check
it
out
when
you
have
a
chance:
https://www.studica.com/b
tutorial-
animator-
     controllers)

Unity 3D (http://www.studica.com/unity/unity-5-professional-edition-for-students-and-faculty.html?
ex_ref=news_BLOG&utm_source=blog&utm_medium=blogpost&utm_campaign=UTAnim) has some great
tools for handling animations. In general, developers will utilize animation controllers (Unity calls them
https://www.studica.com/blog/unity-tutorial-animator-controllers 2/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog
animator controllers) to handle which animations to play and when to play them. In this Unity tutorial
blog, I’m going to provide step-by-step instructions to create a very simple animation controller. This

(HTTPS://WWW.STUDICA.COM/BLOG/
controller will be able to play an animation when the user presses a button on their keyboard.
Speci cally, we will make an animation that will make a 3D cube red when the user presses spacebar. It
will return to white when the user presses spacebar again.

What is an Animation Controller?


If you’ve done any 3D modeling you’re likely familiar
with 3D animation. Games heavily use 3D and 2D
animations for models and sprites. The main di erence
between a video game and a 3D animated lm, for
instance, is that a video game must be adaptable to a
wide variety of inputs and conditions. In a 3D animated
lm, the modeler animates every single frame of the
lm in a very speci c way. In a game, this isn’t an option
unless you’re rendering non-playable cut scenes. In a
game, maybe you allow your user to move a character forward, backward, left, or right. Maybe the player
can also run in any of these directions. Maybe the player can crouch and move in any of these directions
at the same time. Each of these states requires a custom animation.

You need animations for the character in a crouching position, crouching and walking forward, sprinting
and moving forwards, walking and moving left, and more. You can’t simply create a single animation
that can somehow tell what your player is doing at any given time. This is where animation
controllers come into play. An animation controller allows you to tell your object which animation
to play based on conditions that you specify. If your character enters a running state, then you can tell
the animation controller to play the animation for running forward, running backward, running right, or
running left, depending on the input of the user. Now, let’s work through this Unity tutorial and learn
how to make a simple animation controller.

How to Make an Animation Controller


I’m going to assume you know most of the basics of Unity (http://www.studica.com/unity/unity-5-
professional-edition-for-students-and-faculty.html?
ex_ref=news_BLOG&utm_source=blog&utm_medium=blogpost&utm_campaign=UTAnim), such as
creating Game Objects and adding components to them.

1. Create a new Scene


2. Create a 3D Cube Game Object and position your camera to be looking at it
https://www.studica.com/blog/unity-tutorial-animator-controllers 3/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog
3. Go to the Window menu and click on “Animation”. This will open the animation window
4. In the animation window, click the “Create” button in the middle of the window.

(HTTPS://WWW.STUDICA.COM/BLOG/

5. A window will come up asking you to specify a lename. Name it whatever you want. I named mine
“RedAnim”.
6. In the animation window, click the “Add Property” button. Choose “Mesh Renderer”. Find
“Material._Color” and scroll right in the window and choose the “+” icon to add it to the list of
properties.

7. This is all we need for the base animation. It will ensure that our cube is white.
8. Moving along in this Unity tutorial, we need to make another animation for our base color. Click the
“RedAnim” header button and choose “Create New Clip…” as seen here:

9. Another dialogue box will come up asking you what to save the le as. Name it “Base”. We’re going to
add the same color property again by clicking “Add Property” and choosing the Mesh Renderer →
Material._Color option.

https://www.studica.com/blog/unity-tutorial-animator-controllers 4/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

10. Make sure your timeline is at 0:00 and change the g and b values to 0.

This will make the transition from red back to white look more natural.
11. Now, we need to create the animation controller. Right-click in the project window and click Create →
Animator Controller. Name it whatever you want.

https://www.studica.com/blog/unity-tutorial-animator-controllers 5/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

12. Double click on the animator controller to open the animator controller editor. Once it’s open, drag
and drop both of your animations into the animator controller window.

https://www.studica.com/blog/unity-tutorial-animator-controllers 6/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

13. The next part of this Unity tutorial is to create transition logic for our animations. Right-click on “Any
State” and choose “Make Transition”, Move your mouse on top of the “Base” node and left click on it.
This will connect an arrow from Any State onto the Base Animation, like such:

https://www.studica.com/blog/unity-tutorial-animator-controllers 7/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

14. Now, we want to be able to transition from our Base state to our RedAnim state. Right-click on “Base”
and choose “Make Transition” and click on “RedAnim”.

15. Now, we also want to be able to go back to our base state from RedAnim. So right-click on RedAnim
and choose “Make Transition” and connect it to the “Base” node.

https://www.studica.com/blog/unity-tutorial-animator-controllers 8/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

16. This owchart is basically what we’re looking for. However, we still need to create a variable that we
can use in our code to be able to make the transition happen. To do this. Click the “+” icon near the
top-left of the animator controller window and choose “Trigger” from the drop down:

17. Name the trigger “MakeRed”. We’ll be able to call on this trigger via C#. However, we need to tell the
transitions that we want to use this trigger to make a transition happen. Left click on the arrow going
from base to RedAnim. You’ll notice the transition come up in the inspector on the right side of
Unity. There is a section called “Conditions”. Click the “+” icon in this section and it will add the
“MakeRed” trigger to the list of conditions.

https://www.studica.com/blog/unity-tutorial-animator-controllers 9/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

18. We also need to do this on the transition from RedAnim to Base. Left-click on that arrow and do the
same thing. Now Unity will know to only make these transitions happen if the MakeRed trigger is
activated. Notice how we use a single trigger for both transitions. This works because each
transition will only happen if we are already in that transition. So, if we are on the Base animation,
then MakeRed can only cause the transition from Base to RedAnim to happen. The same is true for if
we are on the RedAnim animation.
19. At this point, we need to put the animator controller on our cube. Drag and drop the animator
controller you created into the cube object in the inspector. The cube’s component list should look
like this:

https://www.studica.com/blog/unity-tutorial-animator-controllers 10/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

20. We need to change one more thing on our animations before we create the script. Left click on the
animation in your project window. In the inspector, uncheck the “Loop Time” box. This will ensure
that looping is turned o on these animations. Do this for both animations.

https://www.studica.com/blog/unity-tutorial-animator-controllers 11/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/

Note that you may not always want to disable looping, depending on what you intend for your
animation.
21. Now you need to create a new C# script named RedScript. This script is incredibly simple. It looks like
this:

22. All this script does is grab an Animator component on the object. It then listens for the user to press
Spacebar. If spacebar is pressed, then we set the MakeRed trigger, which will then re o our
animation transitions.
23. Save the script and attach it to the cube in your scene. Now, if you play the game and press spacebar.
You should see the cube change to red. If you press spacebar again, it will change back to white. You
can do this endlessly and it will keep making the same transitions occur. If you leave your animator
controller node tree open while the game runs, you can see how your animations are
behaving. This can be helpful for troubleshooting potential issues with your animation logic.

Summary
https://www.studica.com/blog/unity-tutorial-animator-controllers 12/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog
I hope you have enjoyed this Unity tutorial. Obviously, this is an incredibly simple animation controller
but hopefully, it shed some light on how they work. To me, one of the most powerful features is the

(HTTPS://WWW.STUDICA.COM/BLOG/
ability to call on Triggers or other animator controller variables via script. This allows you to have massive
control over the ow of your animations depending on any number of factors in your game. Hopefully,
you found this information helpful. Be sure to check out Studica.com (http://www.studica.com/?
ex_ref=news_BLOG&utm_source=blog&utm_medium=blogpost&utm_campaign=UTAnim) for academic
discounts on software (http://www.studica.com/software.html?
ex_ref=news_BLOG&utm_source=blog&utm_medium=blogpost&utm_campaign=UTAnim), electronics
(http://www.studica.com/hardware-electronics.html?
ex_ref=news_BLOG&utm_source=blog&utm_medium=blogpost&utm_campaign=UTAnim), robotics
(http://www.studica.com/Robotics.html?
ex_ref=news_BLOG&utm_source=blog&utm_medium=blogpost&utm_campaign=UTAnim) and more.
Check out more Unity tutorial postings (http://www.studica.com/blog/?s=unity+tutorial?
ex_ref=news_BLOG&utm_source=blog&utm_medium=blogpost&utm_campaign=UTAnim) on our blog.
Good luck!

Blogger: Mark Philipp, Application Engineer at Studica

SHARE THIS POST

https://www.studica.com/blog/unity-tutorial-animator-controllers 13/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/
(mailto:?
subject=Unity+Tutorial%3A
thought
you
might
enjoy
this!
Check
it
out
when
you
have
a
chance:
https://www.studica.com/b
tutorial-
animator-
     controllers)

Tags:3D Animation (https://www.studica.com/blog/tag/3d-animation), 3d modeling


(https://www.studica.com/blog/tag/3d-modeling), animator
(https://www.studica.com/blog/tag/animator), controlled
https://www.studica.com/blog/unity-tutorial-animator-controllers 14/17
9/7/2019 (https://www.studica.com/blog/tag/controlled), game
Unity Tutorial: How to Use design
Animator Controllers and Triggers | Studica Blog

(https://www.studica.com/blog/tag/game-design), game dev


(https://www.studica.com/blog/tag/game-dev), game development

(HTTPS://WWW.STUDICA.COM/BLOG/
(https://www.studica.com/blog/tag/game-development), How to
(https://www.studica.com/blog/tag/how-to), renderer (https://www.studica.com/blog/tag/renderer),
step by step instructions (https://www.studica.com/blog/tag/step-by-step-instructions), trigger
(https://www.studica.com/blog/tag/trigger), tutorial (https://www.studica.com/blog/tag/tutorial),
unity (https://www.studica.com/blog/tag/unity), Unity 3D (https://www.studica.com/blog/tag/unity-
3d)

Select Language ▼

Recent Posts
Mobile Robotics Competition Results – WorldSkills Kazan (https://www.studica.com/blog/mobile-robotics-
results-worldskills-kazan)

Soar into September with Education Webinars (https://www.studica.com/blog/september-education-


webinars-19)

WorldSkills Kazan 2019 is Here #SkillsChangeLives (https://www.studica.com/blog/worldskills-kazan-


2019-here)

Showcasing schertechnik at Advanced Manufacturing Expo (https://www.studica.com/blog/advanced-


manufacturing-expo-2019)

Money-Saving Tips for the College Student (https://www.studica.com/blog/student-money-saving-tips)

Topics
3D Printing (https://www.studica.com/blog/category/3d-printers-printing) (27)

All Topics (https://www.studica.com/blog/category/all-topics) (704)

Animation and Storyboarding (https://www.studica.com/blog/category/animation-and-storyboarding)

(14)

Architectural Visualization (ArchViz) (https://www.studica.com/blog/category/architectural-visualization-


archviz)

(25)

Coding & Programming (https://www.studica.com/blog/category/coding-programming) (23)

https://www.studica.com/blog/unity-tutorial-animator-controllers 15/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog
Competitions & Contests (https://www.studica.com/blog/category/competitions-contests) (59)

(HTTPS://WWW.STUDICA.COM/BLOG/
schertechnik (https://www.studica.com/blog/category/ schertechnik) (94)

Game Design & Programming (https://www.studica.com/blog/category/game-design-programming) (1)

Industrial Simulation & Training (https://www.studica.com/blog/category/industrial-simulation-training)

(3)

Internet of Things (IoT) (https://www.studica.com/blog/category/internet-of-things-iot) (6)

Language Learning (https://www.studica.com/blog/category/language-learning) (9)

Manufacturing Technology (https://www.studica.com/blog/category/manufacturing-technology) (7)

National Instruments (https://www.studica.com/blog/category/national-instruments) (59)

New Releases (https://www.studica.com/blog/category/new-releases) (86)

Pantone & Color Management Tools (https://www.studica.com/blog/category/pantone-color-


management)

(12)

Robotics (https://www.studica.com/blog/category/robotics) (39)

Statistics & Analysis (https://www.studica.com/blog/category/statistics-analysis-ibm-spss) (6)

STEM Education (https://www.studica.com/blog/category/stem-education-2) (103)

Student Discount (https://www.studica.com/blog/category/student-discount) (18)

Support (https://www.studica.com/blog/category/support) (37)

Tutorial (https://www.studica.com/blog/category/tutorial) (51)

Unity Game Development (https://www.studica.com/blog/category/unity-game-development-platform)

(78)

Webinars (https://www.studica.com/blog/category/webinars-2) (72)

WorldSkills Robotics (https://www.studica.com/blog/category/worldskills-robotics) (17)

Archives
Select Month

https://www.studica.com/blog/unity-tutorial-animator-controllers 16/17
9/7/2019 Unity Tutorial: How to Use Animator Controllers and Triggers | Studica Blog

(HTTPS://WWW.STUDICA.COM/BLOG/
   

(https://www.facebook.com/studica)
(https://twitter.com/studica)
(https://www.linkedin.com/company/studica)
(https://www.youtube.com/user/Studica
(https://instagram.com/studicac
(https://www.pinterest.co
SHOP (HTTP://WWW.STUDICA.COM)
/ STEM EDUCATION (HTTPS://WWW.STUDICA.COM/BLOG/CATEGORY/STEM-EDUCATION-2)
/ ROBOTICS (HTTPS://WWW.STUDICA.COM/BLOG/CATEGORY/ROBOTICS)
/ 3D PRINTING (HTTPS://WWW.STUDICA.COM/BLOG/CATEGORY/3D-PRINTERS-PRINTING)
/ ABOUT US (HTTPS://WWW.STUDICA.COM/BLOG/STUDICA-EDUCATION-TECHNOLOGY)

https://www.studica.com/blog/unity-tutorial-animator-controllers 17/17

You might also like