Join Sue Blackman for an in-depth discussion in this video Adding obstacles and pushing physics, part of Unity 3D: 3.5 Essential Training.
While we were in the Platformer scene, let's look at one more important concept. You've probably noticed that your character isn't able to push aside the rocks. It's not that they have too much mass, it's just that Character Controllers aren't set up to be able to push things. That would require a lot more overhead. We can, however, give him a script that will give in that behavior with a lot less overhead. It finds out what direction he's going when he hit something, then it applies a physics force to the object in the same direction.
This script comes straight out of the docs when you search OnControlColliderHit. Let's go ahead and look for that. Help > scripting Reference, and we're going to search OnControllerColliderHit and enter, and we can see it says it's called when the controller hits a collider while performing a move. This is the code we're going to be using, but it doesn't have any carriage returns. So I am going to go out to the snippets file and get my version. Exercise Files12/12-04/snippets. I'll highlight them, right-click, and Copy.
So if body equals no, that means it doesn't have a rigid body or isKinematic is checked then we're going to return. In other words, it wasn't a valid object. Otherwise, in the next section we are going to make sure that we're going to right direction, and if not we'll return, and then we need a variable for the direction, and we get that from our original local variable hit. Once we have got all that, we put it together, and body--that was the one that got hit--velocity equals the pushDir * pushPower.
Let's go ahead and save this guy and back into the editor, and we'll need to drop it on the Platform Character, and we'll be able to see it down here at the bottom. Next, we are going to need stacked crates to check out our pushPower. Let's import the CrateSmall package. Right-click > Import Package > Custom Package > 12-04 > Assets > CrateSmall, and we'll click Open and Import. Let's drag into the Hierarchy view from the Prefabs folder, here it is, and let's go ahead and focus in on it.
Double-click, and now I want to switch back to perspective view, and it seems to be missing its mesh. When this happens, sometimes you can get the mesh back by clicking on the browser and looking for the original object, Crate, and there we go. Let's make sure it's centered, so it doesn't get stuck on the wall colliders, and if I use Alt and my left mouse button it looks pretty good. And I think I want to drag mine down here about like that. And then here's what we are going to do with this guy.
He already has a Box Collider, and if we look here he already has a Rigidbody. In our Platformer level we are going to take advantage of the Rigidbody's constraints to prevent the box from going sideways. First, we are going to freeze the position X, and then we're going to freeze rotation Y and rotation Z. This will make it so our box can only roll along the platform, and it can't move sideways. I am going to spend a few moments here rearranging my power-ups. I am going to move my Key higher, and I've also got my Heart inside my lift.
So I need to get that back, and I want to put it up a little bit higher here, and then I'm going to use Ctrl+D to duplicate my crate a couple of times. Now our little character is going to have to climb up on the crates to get to the Heart. Let's click Play and test. So I am going to move him and jump him, and there we go. He fell off. So I'm going to exit Play mode, and in the Platform Asset Start I am going to turn my invisible walls back on. That should prevent him from falling off.
So now, let's do one more thing. Let's take a minute to go back and finish the DeathZone script. Now that we have objects that can be pushed off the platforms, we'll need to tell the DeathZone to destroy them. Back up in a Game Scripts folder I'm going to open the DeathZone script, double-click it, and in the script editor where I have my comment here, we're going to replace that with Destroy(victim.gameObject), and that should take care of random things like rocks and crates that fall down into the chasm.
I'll save the script and head back to the editor. We now have a very simple platformer where the player can get power ups, lose points, and push things around. Our little platformer scene is ready to become a level.
- Understanding game and level design theory
- Organizing your project in Unity
- Creating and transforming objects
- Setting up the geometry
- Painting in terrain, textures, and trees
- Adjusting the render settings
- Importing terrains
- Creating a first-person controller
- Creating materials and shaders
- Lighting the game
- Working with cameras and multiple views
- Animating characters and assets
- Creating fire with particle systems
- Managing the GUI (graphical user interface)
Skill Level Beginner
1. Exploring Unity 3D
2. Exploring the Terrain Editor
3. Creating the Environment
Publishing project settings3m 37s
4. Introducing Unity Scripting
5. In-Game Scripting
6. Working with GameObjects and Components
7. Exploring Prefabs
8. Using Imported Assets
9. Understanding Lighting
10. Keyframing Animation
11. Animating Skinned Meshes and Controlling Characters
12. Working with Cameras and Layers
13. Creating Game GUIs
14. Extra Techniques and Features
What's next1m 13s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.