39,59 €
A recipe-based guide to give you practical information on Unity 5.x animation techniques and tools
This book is for Unity developers who have some exposure to Unity game development who want to learn the nuances of animation in Unity. Previous knowledge of animation techniques and mecanim is not necessary.
This recipe-based practical guide will show you how to unleash the power of animation in Unity 5.x and make your games visually impeccable. Our primary focus is on showing you tools and techniques to animate not only humanoid biped characters, but also other elements. This includes non-humanoid character animation, game world creation, UI element animation, and other key features such as opening doors, changing lights, transitioning to different scenes, using physics, setting up ragdolls, creating destructible objects and more.
While discussing these topics, the book will focus on mecanim, the Unity 3D animation tool, and how you can use it to perform all these tasks efficiently and quickly. It contains a downloadable Unity project with interactive examples for all the recipes. By the end of this book, you will be confident and self-sufficient in animating your Unity 3D games efficiently.
This practical no-nonsense guide is recipe-based with real-world examples of almost all the techniques mentioned.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 385
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2016
Production reference: 1160516
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-391-0
www.packtpub.com
Author
Maciej Szcześnik
Copy Editor
Sameen Siddiqui
Reviewer
Grzegorz Mazur
Project Coordinator
Bijal Patel
Commissioning Editor
Amarabha Banerjee
Proofreader
Safis Editing
Acquisition Editor
Aaron Lazar
Indexer
Rekha Nair
Content Development Editor
Prashanth G Rao
Production Coordinator
Aparna Bhagat
Technical Editor
Murtaza Tinwala
Cover Work
Aparna Bhagat
Maciej Szcześnik is an experienced game designer and Unity developer, specializing in gameplay and combat mechanics. His daily responsibilities include motion capture session planning and coordination, creating animation graphs and state machines, and AI design and implementation.
He started his professional career in 2004 at CD Projekt RED—the company best known for the critically acclaimed The Witcher series. Maciej had key roles in the Witcher games, being lead gameplay designer and lead combat designer. He also worked at 11 bit studios, another well-known Polish game development company, famous for the This War Of Mine game.
Maciej gave three talks at the Game Developers Conference and Game Developers Conference Europe. He is also a lecturer at Warsaw Film School, teaching Unity and technical aspects of animation as part of the game development BA course.
I'd like to thank my wife for her support, Kacper Kwiatkowski and Grzegorz Mazur from Vile Monarch for their reviews, Marek Ziemak and Piotr Tomsiński for inspiring discussions about games and animations, and Michał Pieńkowski and Marcin Iwanek for countless RPG and tabletop game sessions. I'd also like to thank all my friends and former coworkers from 11 bit studios and CD Projekt RED for helping me develop my skills.
Grzegorz Mazur is a programmer who graduated in information technology from Warsaw University of Technology. He is currently co-boss and technical director at Vile Monarch. Previously, he worked for 11 bit studios, where he was lead programmer of This War of Mine and Sleepwalker's Journey. He specializes in gameplay and AI programming and is a teacher at Warsaw Film School. He is also an amateur musician and board game enthusiast.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Get notified! Find out when new books are published by following @PacktEnterprise on Twitter or the Packt Enterprise Facebook page.
This book describes Unity's animation techniques from a designer's point of view. It is focused on achieving interesting gameplay effects and creating game mechanics. Each topic is presented in the form of a short recipe with a list of steps needed to implement the given feature. All the recipes are prepared in the most condensed form—a good basis to build your own solutions on.
This book doesn't cover animating in a 3D package, although you may find some helpful tips regarding Blender 3D in it. A lot of the recipes presented in this book use C# scripting not only for triggering animations, but also for creating interesting motions and effects. If you are a game designer or a Unity developer and would like to know more about what's possible with Unity's animation system, I hope this book will be an interesting read for you. Also, if you have any questions regarding this book or the topics it covers, you can contact me via Twitter (@MaciejSzczesnik). I will be happy to help.
Chapter 1, Working with Animations, describes the tools essential for importing animations from 3D packages. It also covers most of the import settings.
Chapter 2, Working with the Animation View, covers Unity's built-in animation tool, used to animate almost everything apart from characters.
Chapter 3, 2D and User Interface Animation, focuses on animating 2D sprites and UI elements.
Chapter 4, Character Movement, covers different types of movement, using animations and root motion.
Chapter 5, Character Actions and Expressions, is about actions, action points, and facial expressions.
Chapter 6, Handling Combat, covers combat-specific topics, such as using animations for aiming and creating a hit-detection system.
Chapter 7, Special Effects, is not only about particle systems, but also about animating fog, creating wind zones, and even using sound waveforms for animation.
Chapter 8, Animating Cutscenes, describes how to import a cutscene from a 3D package and how to prepare simple cutscenes using Unity's Animation View.
Chapter 9, Physics and Animations, shows how to create ragdolls and break stuff.
Chapter 10, Miscellaneous, is the last chapter and describes a few solutions to common problems—for instance, working with mocap animations in Blender and Unity.
You need to install Unity 5.x (preferably 5.3 or greater). You also need to have your own animations or download the provided example project. It contains the assets needed to follow each recipe.
This book is for Unity developers who have some exposure to Unity game development and want to learn the nuances of animation in Unity. Previous knowledge of animation techniques and mecanim is not necessary.
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Simply drag and drop the file to your Assets folder."
A block of code is set as follows:
using UnityEngine; using System.Collections; public class Wave : MonoBehaviour { //The anim variable is used to store the reference //to the Animator component of the character. private Animator anim;New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Go to Assets | Import new asset and choose your FBX file."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Unity-5.x-Animation-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Unity5xAnimationCookbook_ColoredImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
This chapter explains the essentials of working with animations in Unity and covers the following topics:
Unity is a great game engine that implements the animate everything philosophy, which allows you to visualize even the most creative gameplay ideas. Through the course of this book, we will learn a variety of recipes that will help you unleash the power of Unity 5.x animation tools and make your games more fun.
In this first chapter, we will get more familiar with using skeletal animations in Unity. It will guide you through the process of importing such animations, editing them, and assigning them to your in game characters. This knowledge is essential for understanding recipes described in further chapters.
This first recipe shows how to import a skeletal animation from a 3D package. It assumes that you have an animation already prepared. We are going to bring the file to Unity and show where you can adjust the import settings. We will not jump into too much detail for now to make the import process as quick as possible. We will cover all the settings in further recipes.
Most animations are created in external 3D packages such as Maya, 3ds Max, Motion Builder, or Blender 3D. Make sure you have prepared a 3D model with a skeleton, the model is skinned, and you have created at least one animation. You can also download the provided example; open the project in Unity and go to this folder: Chapter 01 Working with animations\Recipe 01 Importing skeletal animations\Sheep Model. You will find an imported FBX file called Sheep.fbx there.
To import a skeletal animation, you need to follow these steps:
If you are using Blender, you don't have to manually export the file to FBX format. Simply drag and drop the file to your Assets folder. Unity will use Blender FBX exporter in the background. Additionally, you will be able to open the file by double-clicking on it in Unity. After you edit and save it, Unity will reimport it again. It is a very convenient way of editing animations as you don't have to go through the export-import process each time you make a change.
Unity uses four different rig configurations:
We will discuss differences between Generic and Humanoid rigs further in the next recipe.
If you are using Blender, make sure to rotate the rig -90 degrees in the X axis, apply the rotation in Blender and then rotate it again, +90 degrees in the X axis. The rotation of the rig in Blender should be: 90 X, 0 Y, and 0 Z. Your model should be facing the -Y axis in Blender (when you change the view to FRONT, you should see the face of your character). Blender and Unity use different axes alignment: in Blender, Z is up and Y is back; in Unity Z, is front and Y is up. This little trick solves the problem. Your model after import should have 0 rotation and should be facing the Z axis. It is important for moving objects (such as characters) and rig configuration. If you don't do this, your model will have -90 X rotation after the import.
This recipe shows how to configure two most frequently used rig types: Generic and Humanoid. We will go through all the available options for both of them.
As previously mentioned, make sure you have prepared two animated characters. One of them should be a humanoid and the other a non-humanoid, a quadruped for instance. You can also download the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 02 Configuring generic and humanoid rigs\Rigs directory.
You will find there three FBX files:
If you are creating your characters from scratch, the bones hierarchy of your humanoid rig is important for Unity to recognize the rig as a humanoid. It should follow this pattern:
HIPS -> SPINE -> CHEST -> NECK -> HEAD HEAD -> EYE (for left and right eyes) HEAD -> JAW HIPS -> UPPER LEG -> LOWER LEG -> FOOT -> TOES (for left and right legs) CHEST -> SHOULDER -> ARM -> FOREARM -> HAND (for left and right hands) HAND -> PROXIMAL -> INTERMEDIATE -> DISTAL (for five fingers in left and right hands)The hip bone is the root bone of the humanoid character. Fingers, shoulders, chest, neck, eyes, jaw, and toes are optional bones. Your humanoid character will work without them.
You should also remember to model the character in a T-POSE. It should face the Z axis in Unity (if your 3D software uses different axis alignment from Unity's like Blender, remember about the -90 and +90 degrees rotation trick shown in the Importing skeletal animations recipe). Hands of the character should be flat, palm down, and parallel to the ground along the X axis. A-POSE characters will also work.
To configure a generic rig you need to follow these steps:
Humanoid rig configuration has a lot more options:
Some quadruped characters can be mistaken by Unity for humanoids. You can find a Quadruped.fbx file in the provided example Unity project. Theoretically, you can set its rig to Humanoid, and Unity will recognize it as valid. This, however, is a mistake—all quadruped characters' rigs should be set to Generic. Setting them as humanoids can cause problems later.
Humanoid rig uses more advanced Mecanim features than the Generic rig. You can find the list of such features below:
I highly recommend using Humanoid rigs for all humanoid characters in your game.
Animator Controllers are state machines (graphs) responsible for controlling the flow of animations of any animated object in the game. The same Animator Controller asset can be used by multiple objects or characters. Unity will create an independent runtime copy of the asset for each animated object it is assigned to.
As always, you should have a rigged and animated character ready before we start. Import it into Unity, choose the proper rig type, and put it into a scene. You can download the example Unity project and go to the Chapter 01 Working with animations\Recipe 03 Creating and assigning an animator controller directory. There is a scene called Example.unity there. If you open it, you'll find a Sheep character in the Hierarchy. It has an Animator Controller already created and assigned. You can also use the Quadruped.fbx file from the Chapter 01 Working with animations\Recipe 03 Creating and assigning an animator controller\Rigs directory to follow the recipe step by step.
To create and assign an Animator Controller, follow these steps:
Every animated object in Unity uses an Animator component and an Animator Controller asset. The component is responsible for playing animations in runtime. It has a number of parameters that we have to set or we can use to tweak the component's functionality:
The Animator Controller asset stores a graph of animations (animation states) and defines the rules of switching between them, blending them, and so on. The controller (asset) is attached to the component's Controller field (the component is attached to a character prefab or a character placed in the scene). Many objects or characters can share the same Animator Controller if they use the same animations (have the same rigs or are humanoid characters).
If you want to learn how to create animation graphs and control their flow, see the next two recipes: Creating animation transitions in Animator Controller and Using parameters to control the animation flow.
As mentioned previously, Animator Controllers are state machines containing animations (states). Any given character can be in one such state (play one animation) at a time. To switch between states (animations), you need to create state transitions.
Before we start, you should have an animated model placed on a scene with an Animator Controller assigned. You can find such a model in the provided example Unity project. Go to the Chapter 01 Working with animations\Recipe 04 Creating animation transitions in Animator Controller directory. Open the Example.unity file. You will find a Sheep object in the Hierarchy. It has an Animator Controller assigned. You can open it by double-clicking on the Controller field in the Animator component of the Sheep game object.
To create a state transition in an Animator Controller, follow these steps:
State transitions define how we can travel through the graph of animations. They are combined with transition conditions based on Animator Controller parameters; we will discuss the parameters in detail in the next recipe: Using parameters to control the animation flow. Each transition has a set of properties we can adjust. Click on the transition (white arrow) to select it. You can find the transition properties in the Inspector tab (make sure to unfold the Settings foldout):
