Monday, October 15, 2018

3D Animations 1 / 2

To use animations in unity, you can either make them yourself in the editor, or use a model that already has them. In this example, I used free model with animations from the asset store that you can download here. Alternatively, you can download it straight from the Asset Store, but some assets will have different names.

After placing model in our scene, we should create 'Animator Controller' for it and call it TigerAnimator.
Create 'Animator Controller'

Assign our TigerAnimator to our Tiger model.
Assign TigerAnimator

Now double click on TigerAnimator to open Animator window (you can do that in either Inspector or Project panels).
Animator panel

Find 'idle' animation and drag it into Animator window to create new state.
Creating 'idle' state

If you 'Play' your project now, you should have your model playing animation you picked.
Playing idle animation

Lets add another one. Find 'roar' animation and drag it to the Animator window.
Creating 'roar' state

We need a way to tell our TigerAnimator to play 'roar' animation. To do that, we need to add parameter of type 'trigger', and then use it in transition from idle state to roar state.
Roar trigger

To create new transition, right click on 'idle' state, pick 'Make Transition', and connect it to 'roar' state.
Creating transition from 'idle' to 'roar' state

Select our new transition and add 'Roar' trigger as new condition to it. That way it will play whenever we fire 'Roar' trigger from script. Also disable 'Has Exit Time' option, since we don't want 'roar' state to wait till idle animation is finished.
Setting up transition


Now we can add script to control out Tiger. With our Tiger model in the scene selected, click on The Add Component button and select 'New script'. Lets name it Tiger.
Adding Tiger.cs script
Open our newly created Tiger.cs script by double clicking on it (Visual Studio might take few seconds to start), and make it look like this.


Now when you 'Play' your project, tiger should play 'roar' animation when you press Fire1 button

There is a problem though, our tiger never stops roaring... We forgot to add transition from 'roar' state back to 'idle' state... Lets fix this! Open animation window, select 'roar' state and make new transition to 'idle' state.
Creating transition back from 'roar' to 'idle' state
Now our tiger should roar only once whenever we press Fire1 button.

As an exercise, you can add 'Attack' animation in the same way, just connect it to Fire2 button instead (left ALT key by default).

In part two, we will make our tiger walk around Part 2

No comments:

Post a Comment