How To Play Animation Where You Left Off
There are several means to play an animation on Unity. The virtually mutual one is what will exist taught in this tutorial. Yous'll larn how to use Animator Controllers.
It'due south also possible to utilize the Playable API to achieve this effect. The overhead is style smaller simply this alternative is considered advanced, avoid doing information technology if you lot're simply getting started at C# scripting on Unity
On that puzzle, there are a few pieces that demand to be understood. Offset, let's see a step-by-footstep process on how to play an animation and the description of each of these pieces.
This tutorial has been adult and tested using Unity 2018.2
A step-by-footstep procedure on how to play an animation
- Import the 3D model
- Import the animation (or create it inside Unity)
- Create an Animator Controller and configure it to accept a trigger parameter.
- Utilize the component Animator in a script to activate the trigger create on three.
If you want to practise that for a 2D blitheness the process is slightly different, but this tutorial should you help on that job too.
For steps i. and ii. information technology's sufficient to drag and drop the avails to your Avails directory of your project that Unity volition practise the proper import. You tin as well download directly from the Nugget Shop.
Earlier going to steps 3. and 4. allow'southward ameliorate sympathize what they are about.
What is an Animator Controller?
Unity utilizes a sophisticated system of control, transition and blending of animations. In the past, this system was largely known every bit Mecanim.
For playing just a single blitheness information technology looks like a lot of work for a small return using this sophisticated system. When you lot outset to employ various animations for the same graphic symbol and desire the animations to smoothly blend from one some other, you'll before long realize that this workflow is indeed really interesting and effective.
The center of this command is divers by the Animator Controller. An Animator Controller is an asset containing all the information on how to manage and blend the dissimilar animations your character (or object) posses also as the transition between them. These transitions are described through a finite state machine. Do non be scared since to what nosotros'll build here the machine volition be really unproblematic.
saved animator controller asset
Se você já possui os seus clipes de animação importados, o Animator Controller os united states para controlar qual (quais) animações vão ser executadas e em que momento dependendo practise atual estado deste Animator.
If you already have your animation clips imported, the Animator Controller will use them to control which animation (animations) will be executed and when depending upon the state of an Animator.
States
You'll take states like idle, walking, shooting, expressionless, etc. Betwixt these states the transitions. In general, a character starts on an Idle land and might change information technology to some other based on a parameter and transitions conditions.
A state car with us:: Idle (default state), Walk, Run and Spring .
Parameters
The parameters of an Animator Controller are the variables controller via script that allows you to interact with the animator. Going back to the Idle case -> Walking, we could accept a parameter speed and if speed is greater than or equal to 0.1 so we would transition from Idle to Walking.
There are iv types of parameters:
- Integer. It's a whole number.
- Float. A (approximated) real number.
- Bool. A true or simulated.
- Trigger.A special true or false which turns off automatically.
As they saw that the parameter bool was existence used almost of the time turning information technology off correct abroad from true to fake, as in the case of a shooting animation, the unity developers create the trigger type that does this kind of work to u.s. automatically.
In that tutorial, we'll apply a trigger parameter.
Transitions Nuts
Parameters are the base of operations of transitions. For each country, y'all'll create transitions betwixt them and the conditions for each of these transitions.
On that example nosotros created the transition Idle -> Walk in the instance of the parameter "Horizontal" is bigger than 0.1.
Transitions might too happen automatically in the course of time. This option is called Go out Time. On settings, information technology'south possible to control the Go out Time.
Note that Exit Time does not use absolute values in seconds, but relative values from 0 to i, that is, if we have an get out time value of 0.viii, in the first frame in which lxxx% of that blitheness is executed the transition will start.
Creating the Animator Controller We Need
An Animator Controller is an nugget that needs to be saved in your project. Right click on your project tab and look for Create -> Animator Controller, requite a proper noun for your animator controller and press enter.
Yous tin create states for your Animator Controller by dragging and dropping animation clips from the Project tab into the Animator window (opened through a double click on the asset file) or by using the right push Create State -> Empty.
Animation States
Elevate & Drop your animation within the Animator window to create the state of your animation.
On that example, I've likewise dragged & dropped an animation for the Idle land so the grapheme looks more natural while standing nevertheless.
Parameter
Now that nosotros accept us it's necessary to create a parameter of the trigger type that will be used in the Idle -> Attack transition.
Click on the + button of Animator window the superior left.
Choose the type Trigger and proper name information technology Punch
Transition
If your Idle state is non orange correct click information technology and choose Ready as Layer Default Land. With that done press the right push of your mouse on the Idle state and cull Make Transition, at present choose the target state.
transition created and selected
With the outbound transition selected, in the Inspector tab (outside the animator window), add together a condition pressing the + push. Since you have only a unmarried parameter your transition condition is already configured. Exist sure to uncheck the Has Exit Time selection simply above.
Creating and configuring the character to apply this Animator Controller
Drag the asset of your model into the Hierarchy tab or inside the Scene window. In the Inspector add the component Animator. Now drag & driblet the controller that you only created to the field Controller
You may need to ready an avatar for your model. If you downloaded it from the asset store it's highly probable that your avatar will come up ready to be used. You only demand to click on the trivial circumvolve to the right of the avatar field and choose the appropriate one.
Creating the script and triggering the trigger
With the Animator configured for your character, nosotros finally reached the last step where we trigger the trigger via script. Select your character in the Hierarchy tab, choose Component and type something like PlayerController, cull Script and press Create and Add together.
It'due south e'er a good idea to leave your projection organized. Move that file you just created in the Project tab to some other directory where you concentrate all your scripts.
In order to use the Animator component nosotros created, our script needs to make reference to that component somehow. Create a public variable of the type Animator in your script chosen animator.
using UnityEngine; public class PlayerController : MonoBehaviour { public Animator animator; // Use this for initialization void Outset () { } // Update is called in one case per frame void Update () { } } And so elevate & drop the component Animator to the field Animator of your new script.
We'll choose the J key to trigger our animator controller. Inside update, we must verify if the player pressed such key.
void Update () { if (Input.GetKeyDown(KeyCode.J)) { animator.SetTrigger("Punch"); } } Input.GetKeyDown will just return truthful in the verbal frame the thespian started to printing the primal. This prevents the animation from restarting on every frame what would happen if nosotros had used the method Input.GetKey.
Final Result: unity-chan (Unity'south Japanese mascot) executing a 2 punches animation.
Where to go now?
First-class! Yous're now able to play your animations using the unity'south animation system. But I remember that you desire to go on on your journey, right? This system is really rich, I'll propose a few exercises for your continued exercise:
- Make the back transition to
IdlefromAttackusing Go out Time - Create more states and its transitions with at least 3 animations.
- Explore the official Unity documentation well-nigh the subject field always trying to replicate what is beingness explained.
If you have any questions leave them in the comments section below. If you liked this tutorial your friends might similar it too, share it with them!
Classify in:
- unity
- animator controller
- tutorial
- getting started
Source: https://bladecast.pro/unity-tutorial/how-to-play-animation-pressing-a-key-unity-2018-animator-controller-trigger
Posted by: clementsmadmily.blogspot.com

0 Response to "How To Play Animation Where You Left Off"
Post a Comment