From the course: Unity: Architectural Visualization Interactivity

Preparing interactive objects - Unity Tutorial

From the course: Unity: Architectural Visualization Interactivity

Start my 1-month free trial

Preparing interactive objects

- [Instructor] In this chapter we're going to focus on object interactions, in particular opening and closing doors. We're going to give the viewer the ability to click on a door, to open it, to walk through to the other side. This is an example of an object interaction but for your visualizations potentially they could be many other types. For example, maybe you want windows that open or close, draws, desk drawers, or maybe you want to click on a button to summon an elevator. They all have a common pattern however, the pattern is this, you click on the element and something happens in response. These are examples of object interactions. We're going to get started at creating opening and closing doors, and to begin this process, I have opened up the first person scene that is included inside this project. There's nothing special about this scene, it's simply the lobby environment and included in that is the first person controller already built into it so that we can walk around in first person. It doesn't contain other elements of work we have created previously like the message notifications or the teleporters, that's so that we can focus exclusively on creating object interactions. To get started I'm going to select the door that is over here by the staircase, you could choose any of the doors in this environment but I'm going to be focusing on this one for opening and closing. When I left click to select that door, you can see that it selects the door panel object that is here. Now notice the pivot point for the door panel is on the bottom left hand side of the door, which is a really convenient location. If you don't see the pivot point down there by the hinge, it might be because you're using centered mode it may look instead like this. There's a button up here and it might say centered, if it does, you want to click that so that it says pivot, so that we're looking at where the pivot point is for the selected object. If I access the rotate tool with the E key or select the rotate tool here, it's really easy to rotate the store around its hinge and that is perfect. Now because we're going to be animating this door, pre animating its opening and closing animations, I want to make sure that the door when selected from the inspector is not, and I repeat not marked as static since this is a kind of thing that can animate and change at runtime it can't be static. I'm going to make sure that's deactivated by removing the tick from there, yes I do want to change children so that the children objects can move with it too. Great, I'm going to move to the animation tab down the bottom to begin creating an animation for the door, if you don't see the animation tab, it's not a problem you can display that easily from the menu here by us using window animation and then selecting the animation window. You can also access that with the keyboard shortcut controlled six on a PC, command six on a Mac. I'm going to move down to the animation window and then select create here to create a new animation. And I'm going to choose anim door open and choose save. And I just noticed here that actually it's going to save that animation data here inside the scenes folder, that's not what I want, I don't want it inside scenes. So I'm going to grab the anim door open and the door panel, both of which it created automatically for me just now, select both of those elements and drag and drop them into the animations folder. Again, the magic of unity allows you to completely relocate your assets, and it's smart enough to figure out that you did this. Okay, so I'm going to go back and select the door panel from the hierarchy and move to the animation tab to create our door open animation. This is really simple to set up, all I need to do is to move to the animation panel here, click on the add property button, and then from here I want to animate the rotation field that's part of the transform component. So moving to rotation, click on the plus to add that animation tracking to here, I can see the key frames created automatically at the beginning of the animation and also at the end. Now their starting state is correct, the door is closed at the beginning of the animation but at the end it should be open. To do this I'm just going to activate the record button and then click and drag my time slider to the end of the animation, and then simply rotate the door into its open position. Now it's recorded all of that rotation data, you can tell this because when you go to the transform component in the rotation field you'll see that this is highlighted in a kind of pink color to indicate that it's recorded that data. I'm going to deactivate recording here, and then if I click and drag through the timeline you can see the door reaching it's open state. And that really is all there is to creating a door open animation. Great, the animation is recorded, but of course by default, this is going to play whenever I choose play on the toolbar right at the beginning. And not only is it going to play automatically which is something I certainly don't want, but it's also going to play on a loop because whenever you create new animations in unity, by default they play on a loop. Let's deactivate the looping feature first of all. So I'm going to go to the project panel and make sure that I select the door open animation here in the project panel. From the inspector you can see the loop time field is activated, I'm going to deactivate that, I don't want it to play on to a loop. I'm going to select the door once again and the animator component here has a controller field. This contains our animation controller asset which chooses when this animation should and should not play. I'll double click that, and we can see the animation graph which we've seen already on this course back when we were creating the animated notification messages that pop up inside the UI. In this case, when the scene begins, the green entry point note is activated and the flow of the animation moves towards that door open animation, and that's what causes the door to open as soon as we hit play, I don't want that, I want the door to remain closed until we tell it to open. To do that I'm going to right click in the background here, choose create state, and then create new empty state. I'll select that state and I'll rename it to closed to indicate that it's the closed state of the door, and then right click on the close state to set that as layer default state. It turns orange and you can see the connection move from the entry point node to the close state, perfect. The next thing I want to do is I need to wire up the closed state to the door open state to ensure that the door will eventually open when we tell it to. So I'm going to move the door open down here to the left hand side of closed, and I also need to create a parameter that is going to be the switch telling it when to open. I'm going to click on the plus icon to create a new parameter and I'm going to select trigger to add a new trigger parameter and name this door change. And then I'm going to grab the close state, right click, choose make transition, and then connect that to the door open state. I'll select the connection because this is what decides when the door opens. In the conditions list, I'm going to click the plus icon to indicate that when the door change tick box is activated, the door will open. I'm going to deactivate has exit time as well, this makes sure that when this switch is activated, the door will open immediately. Now in the open state, the door open is going to play once and then it's going to hold that last frame, keeping the door open. But of course, I might want to close the door again, in which case I'm going to select the door open animation and duplicate it, may by pressing control D on the keyboard on a PC, or command D on a Mac to create a duplicate. I'm going to select the duplicate and name this door closing and effectively take the animation speed and play that backwards, because the door open played backwards is a door closing. So the connection is going to be between the door open state and the door closing state. When the door is opened, I'm going to right click and choose make transition to the door closing, and when that switch for door change is activated again, the door is going to go back to its closing state. I'm going to make sure that has time exit time here is deactivated too. Now of course, once the door is closed and back in its closed state, it's going to lead around back to the closed state at which point, if door changes hit again, it will go back into this triadic connection of nodes, allowing the door to open and close on demand. Now I'm going to test out these animations, it might be that based on the speed settings here in the speed field, the animation plays back too fast, but we've seen already how we can slow that down, we might need to do that. I'm going to go back to the scene tab, take a look at the scene here, and actually I'm going to grab the animator field and drag and drop that side by side so that I can see both in game tab and the animation tab here side by side, allow me to preview the results. I'm going to press play on the tool by in fact before I press play, I'm going to select the door panel so that when I've selected it I can see its properties here in the animator. By pressing play, it will give me access to the first person controller that I can move around the scene, that's great. In addition to that, you can see from the animator that the door closed state is looping here. So I can now put a tick in the door change marks for the door to open, that's great. And you can see it's also in the open state. I'm going to activate door change again and this time the door closes and it ends up back in the closed state, can press open again, and the door opens. And actually I think that speed is looking just about right so that's a pretty good speed. And about one second it takes the door can open and the door can close. Great, we have now configured the animations for door opens and door closes, of course we don't have the ability to let the user interact with the door yet, when the user clicks on that door, it won't open, but we have the animations in place to begin creating that process. In the next video, we'll look at getting started, and how to create that interactivity.

Contents