Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We can use colliders to activate animations, and trigger things. Beyond simply fencing the player in, colliders can provide a way for the player to interact with the game in their environment. What I've done so far is to add on the master building, all kinds of different box colliders. They're on all the different objects, such as the columns, the railings, and spanning the windows. The only place for this is a mesh collider is on some of the white walls inside. As being just a couple of polygons, those were cheaper as a mesh than as a box.
Everything also though is done with the box colliders, so that the player can no longer walk through the walls. What we'll do now is add a box collider in as a trigger, to open French door 11, the left most door of the fore scene here. This way, the player can finally get out onto this balcony, as one door is unlocked. We could have multiple doors swing open, and do it with either multiple colliders, or a script that triggers all kinds of animations. But for the moment, I'd like to keep it simple, and to hem the player in, to add a little more anxiety finding all the doors but one, locked.
What I've done then, is to take this object, French door 11, and to add on an animation component. This animation is an older component, but it's useful for simple animations. What it lets us do, then, is say, there is an animation on this object and, does it play automatically? In this case, there's an animation on here called French door 11 swing. The door swings open holds for a few seconds and swings closed. Now I'm ready to get a box collider in and to do this I'll make a new game object. I'll choose game object create other and cube.
I'll take this cube, move it over into the space, forward onto the door, and scale it out to the size I need. I'll sit it in the middle, press r to scale it up and scale it out a little bit more so that if the player triggers it from either side, hopefully, they won't smash into the door. The door will actually open in time. I'll even push it out just a little bit more in the door swing, so that before they are smacked by the door, the door should open because of the collider. I'll name this, door open, or something similar and that way, I can find it here in the hierarchy.
I've called it door opener 11 and that way it's associated with French door 11. It's up to you how you'd like to name things so you can find them. But as long as you're using a consistent naming convention, that should be okay. Keep in mind, in many games, you may have an alphanumeric naming convention that really doesn't read well in plain English, but when we're dealing in thousands if not millions of Assets, we need someway to organize. Now I'll turn off the Mesh Renderer on this. Dragging down the preview window a little bit on the material and turning off the Mesh Renderer and turning off Cast Shadows and Receive Shadows as well, this way it absolutely will not participate in the rendering or display, but simply trigger that animation.
Now, I'll set it as a trigger by checking Is Trigger in the Box Collider component, and I'm ready to get an Activate Trigger script on. I'll scroll down to the bottom of the Inspector, click Add Component, choose Scripts and Activate Trigger. These are ready-made scripts that come with Unity that we can use to activate simple things. This is a terrific way to get into scripting. Rather than simply jumping headlong into writing code, you can look at a script like this. Open it up in it's a C# script, we can tell by the icon here and see what it does.
It's a neat way to start to learn the syntax, and how the code is thinking. Then you can start copying and pasting, and writing your own. Now, I'm going to set this up to animate that door. First, I'll put the target in. For my target, I'll click on the Pick icon, make sure I'm in the Scene view and filter down to find that french door. Putting up in the filter in the top FRE and there's French door 11. I'll select it. And now I need a source, which is going to be our First Person controller. Again I'll click on the Pick button.
Make sure I'm in the scene view and type in FI for first person controller. With that selected, I can set a repeat trigger. This way we can go back and forth in the door. It's a trigger count of 1, so if we're counting how many times this is accessed later in a script, that's our value for it. Lastly, I need to set it to animate. We have choices here in the action. Are we triggering something. Replacing. We can use this for example if we are destroying an object. You collide with, destroy, replace it with something else.
Activate or enable, animate and deactivate. These different choices let us do different things such as well, replacing things enabling or, letting something come to life, deactivating, turning it off so we can no longer interact and so on. I'll choose animate and now it should work. Animating that door when the first person controller collides with it. I'll drag my first person controller over closer in this scene, because now there's colliders on all the doors. And then I'll press play, and see how this looks. I put the first person controller in the adjacent Bay in the lobby.
And I'll see how this plays, by pressing play. Spin around, walk forward. Still need to light this space, but at least I have the cam lights. Over we go, and there's the door opening. Go right through, wait a second, okay. So here's the neat part to it, we get close and squeeze through, but we're stuck in the width of the door. This is a neat exercise in realizing the size of the game controller versus the size of the animation and the size of the objects.
The game controller is well, a rather portly fellow actually. If we look at the capsule, it's actually very wide. So, I'm going to reduce the size of that capsule just a bit. And this way, I can keep my doors feeling human scaled. Here's my first person controller. And there's a radius and a height. I'll pull this radius back down from .4 meters to .25. It'll make the collider a little skinnier and should let us get through that door. Much better. When we're not quite so round in the middle, we can get through a single door nicely.
This is an important point here. You maybe looking going, wait a sec, I thought the game controller was just fine as it was. The lesson to learn from that exercise is that we often need to look at who we are as a player, and the world were in to make things feel a certain way. In this case, I'm shooting for a very realistically modeled space, that I've got a single door and these are 3 foot wide by the way. And so if a person can't fit comfortably through a 3 foot wide door, I have to consider either the nature of the person or the nature of that space and adjust.
Now there's not a bias here in anyway, but simply what I'm saying is, our game controller starts out rather oversized. Now we need to take into account who we are in our space, and possibly adjust who we are essentially to fit our environments. So that the game play feels, In this case, more natural. I'll try it one more time. Coming up to the door, it opens, and I walk through comfortably. A little adjustment in the size of the game controller worked, and there's the door swinging closed, and I can trigger it again.
Now, the fun part is the player will get up to the other doors, try to open them, and can't and we'll have a little fear introduced in this situation. I've used that box collider as a trigger then. Activating that animation and the animation takes care of opening and closing the door as if there's a self closer on it. Alternately, we could have it trigger different animations from either side. We can get more complex with multiple colliders or find other ways to trigger an entire tree of behavior. For now though, a simple collider like that, opening up that one door will work nicely in our gameplay.
Get unlimited access to all courses for just $25/month.Become a member
107 Video lessons · 33203 Viewers
94 Video lessons · 23036 Viewers
78 Video lessons · 9428 Viewers
147 Video lessons · 3160 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.