Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Physics in a game engine provides a terrific way to add interactivity and fun in the environment. What physics lets you do is attach rigid bodies or similar colliders to an object and tell them yes, please react when you're hit. You have a mass and so forth. And so you can do things like knock over art, for example, in this gallery. We can also do things like crash into door and fling them open. And even get as far as dealing with hinged objects and chains and so on. We'll start out by putting some art into the gallery that we can knock over.
I've brought in a new FBX called Tower Sculpture. And it's exported out in centimeters so the scale of 0.01 is correct. It's three abstract, modern, tower sculptures made of plaster. And we're going to place them in here, get some colliders on, and let the player knock them over. What I'll do to start is take this tower sculpture and drag it into the scene. I'll put it in, spin around, and move it through the doors. I'll press F to focus in. And there's my sculpture. This is a place, a lot of times, where I'll turn off the lighting, and that way I can see things in a default shading mode a little more clearly.
Because I haven't really lit the place yet. I'll pull this sculpture into place where I'd like it. Probably over here, right in the sun and by this big wall. Now, we're going to let the player knock these over, so we need to add a couple of components on. I'll pick the first tower sculpture. And add a box collider. When you're dealing with physics, two mesh colliders can't collide together. So we need to use other colliders. And this is where, if you need child colliders, such as multiple boxes, to approximate the shape of an object, you can use them.
These towers are close enough to boxes. And have enough edges that stick out, that I'll use a box. There's one and two, and finally, three. Now, with all of them having a box collider, I need to put one more component on. I'll put on a rigid body, choosing under Component>Physics>Rigid Body. The rigid body, then, specifies a mass, a drag, an angular drag. The possibility of using gravity is kinematic and we'll deal with that if we have animation that needs to turn off and on and then react with physics.
And then we have different interpolation and collision detection methods. We'll use the default here, no interpolation and discreet collision. Basically what that says is, when you are collided with, please do something. And that's different form continuously looking or looking dynamically and some other different ways of checking. We're going to say it's discreet because it's when the player knocks into this object, it will knock over the art. Under constraints in the rigid body then, we can say, well, you can rotate. And maybe on a certain axis.
Or you can't move, but you can only rotate. And this is handy for making things that are hinged, for example. I'll leave it alone, because I'd like the player to be able to accidentally knock over the art. A big no, no in an art gallery usually is to touch the art. And so, we're going to let these be here and the player can bumble into them. I'll add a rigid body onto the other pieces choosing Component>Physics>Rigid Body. And one more time. Component>Physics>Rigid Body. This works so far, and everything is set. I want gravity on, and I'm going to leave the mass alone and see how it behaves.
We're going to replace that with this character controller script. What I'll do is open up that Word document and just copy and paste over. I've opened up the Word document from the scripts directory in the project assets. And this is what the script looks like. In scripting, in short, because we'll get into more scripting later, when things are preceded by a double slash, they are commented out. And therefore, the lines of the script we're looking at to start, are variable push power. How much power does the character have? Function on character controller collider hit, when we hit something, do something.
We have an attached rigid body. Basically what that says is, when you hit something you have a rigid body because the character controller does not have one initially and then, if you hit it, what do you do? What it also says is, we have a variable push direction in a vector three. That's our x, y, z. And this is restricted, so we don't push things that are below us. And then, the last part of it, apply the push. The velocity of the character, plus the push direction, times the push power.
I'll try this out again, and what I'll also do is put a light in so I can actually see what's going on. I'll choose Game Object > Create Other > Point Light. I'll move this point light over. As long as I've got something to see beyond grey, that will work. I'll pick my character controller, and there's a push power of two. I'll try it out at four and see if this works. I'll press Play and go knock over the art. It worked. I jumped over one and knocked into the others. And they fell over. I'll try this one more time from a different angle and see how it looks.
I'll press Play, go into my scene, come around to the other side, I'll go look at these neat modern sculptures and accidentally, whoops, knock one over. Now, this should freak out the player just a little bit, which should bring a smile to you as the game designer, because what do we do in an art gallery typically? We look. And so to have somebody be able to knock it over and not know how to pick it up adds a unique element to this game. And more importantly, we've put some physics in our world. What we've done is not just put a collider on where we can just simply bump into them and stop.
But we've told them, yes, please react. And you've got a rigid body so you fall and don't break, and you don't bend. And react with gravity. And so we can make it fall over and now we have fallen art in the gallery.
Get unlimited access to all courses for just $25/month.Become a member
107 Video lessons · 31700 Viewers
90 Video lessons · 21972 Viewers
78 Video lessons · 8047 Viewers
83 Video lessons · 7754 Viewers