From the course: Unity: Interactivity for AEC

Create animation triggers - Unity Tutorial

From the course: Unity: Interactivity for AEC

Start my 1-month free trial

Create animation triggers

- [Instructor] In Unity, an animator controls the motion between animation clips, and we can use this to trigger animation interactively. So our next step is to take our animation clips and connect them to the animator, and then set up what are called triggers that will allow animation to happen. So I'm going to start off on this door here, and we have two animators here. We have one that was created for the door, and one that was created for the drawer front, which would be this object here and this. So we're going to start off with the base cabinet A door. So if I double-click on this animator, it will bring me into the animator window. Now if you don't have that window, you can always find it here under window. But if you notice, this is kind of a graph editor sort of window here. And so we've got our door closed is already set up as our first clip. But what we really want to do is we want to switch between door closed and door open. So I've got these clips here. This is my door closed clip, this is my door open clip. So I'm going to go ahead and select open and drag it in and now I just need to connect these two and then create what are called triggers. So the first thing I'm going to do is hover over door closed, right-click and select make transition. Now this gives me a little arrow here. And I'm going to connect that arrow to door open. And then I'm going to go back the other way. So I'm going to right-click here, go make transition back the other way. Now if you click on these, you'll notice that they highlight in blue. And you could see here that we actually have a little track editor where we can actually control how it flows from one clip to another. And this is really where our animation is going to happen. So if I were to hit play, you'll see that this animation starts to cycle. Okay, so it goes from closed to open and back. And the length of that animation is controlled here in that transition. So if you want to make it longer or shorter, you can do it here. Now we really don't want this to just keep opening and closing, we want to be able to control when that transition happens. So what we need to do is create what are called triggers and then attach them to the transition. So here in my animator window, I have two tabs. I have layers and parameters. So I'm going to go over to parameters and just hit plus. And you'll see it gives me four different options. Float, int, bool and trigger. Now these can be variables that you can use. But the one I really want is called trigger. So I'm going to go ahead and create a trigger. I'm going to call that trigger close. And then I'm going to create one more and we're going to call it open. So what I want to do is basically have these triggers that when you touch that trigger it opens it or closes it. So we can do that, simply by selecting the transition. So the transition it goes from closed to open, will be attached to the open trigger. And we can do that here, over in the inspector. So the first thing I want to do is click off has exit time. And then down here I have conditions. So this will go when this condition happens. And the condition we want, I'm going to hit plus, is the open condition. So now, when that trigger open is set, this will open. And let's do the same for the other one here. So this time we want the trigger to be closed, so I'm going to turn off has exit time, hit plus and then close is the default so we're good there. So now, when I press play, nothing happens. But I have these triggers here, so if I click on open, it opens. If I click on closed, you can see that I can control these by clicking these triggers. Now these triggers can be referenced by a script that will give us the interactivity that we want. So I'm going to go ahead and do the same thing for the upper drawer. And I want you to go ahead and do the same. And then we'll go ahead and proceed from there.

Contents