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.
Now let's tab back to the editor, create a new script, and name it Platformer_Push. In the Game Scripts, right-click > Create > Javascript Platformer_Push and Enter. We'll go ahead and open it up in the Script editor, and we can just replace what's in there and use Ctrl+V to copy in our new script. The first of var pushPower, that's going to be the force we are going to use, and then on the OnControllerColliderHit function, we're going to feed in a local variable hit, and that's the ControllerColliderHit type, and from that we're going to get the body or the rigid body of the object that was hit.
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.
Author
Released
7/19/2012- 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
- Building projectiles with JavaScript
- 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
Duration
Views
Related Courses
-
Game Character Creation in Maya
with Chris Reilly2h 58m Intermediate
-
Introduction
-
Welcome55s
-
Unity basics1m 8s
-
Using the exercise files1m 29s
-
-
1. Exploring Unity 3D
-
Exploring the interface4m 28s
-
Organizing your project8m 12s
-
Exploring Scene view3m 14s
-
Navigating Scene view2m 52s
-
Creating objects7m 38s
-
Transforming objects8m 16s
-
-
2. Exploring the Terrain Editor
-
Painting the topography3m 56s
-
Painting trees and forests4m 56s
-
Painting detail meshes5m 4s
-
Adjusting terrain settings3m 23s
-
3. Creating the Environment
-
Publishing project settings3m 37s
-
4. Introducing Unity Scripting
-
Exploring Unity scripting3m 34s
-
Using variables11m 46s
-
Discovering functions4m 10s
-
Looking at function syntax3m 44s
-
Printing to the console8m 28s
-
Scripting basic functions7m 12s
-
Combining transforms5m 27s
-
Setting transforms8m 34s
-
-
5. In-Game Scripting
-
Raycasting6m 56s
-
Building timers7m 8s
-
Creating a simple projectile13m 16s
-
Refining the projectile5m 32s
-
Sending messages3m 38s
-
Reacting to messages4m 50s
-
Juggling Play and Edit modes7m 33s
-
Suppressing input8m 2s
-
6. Working with GameObjects and Components
-
Creating GameObjects6m 14s
-
Understanding components5m 3s
-
Using colliders for barriers7m 15s
-
Using colliders for triggers4m 28s
-
Exploring physics7m 15s
-
Making cloth9m 56s
-
Working with wind zones7m 50s
-
Using an audio source6m 14s
-
Creating a sound zone5m 34s
-
Adding audio effects6m 31s
-
-
7. Exploring Prefabs
-
Creating and reusing prefabs8m 11s
-
Using prefabs with arrays5m 28s
-
Creating particle systems6m 58s
-
Refining particle systems5m 30s
-
Combining particle systems7m 10s
-
Upgrading weapons6m 15s
-
Exploring water effects7m 53s
-
-
8. Using Imported Assets
-
Importing static objects12m 36s
-
Manipulating textures5m 42s
-
Handling multiple materials5m 42s
-
Animating UVs8m 27s
-
Tracking objects with LookAt5m 45s
-
-
9. Understanding Lighting
-
Investigating ambient light2m 21s
-
Looking at light types3m 13s
-
Exploring shadows5m 23s
-
Baking lighting with Beast8m 16s
-
Experimenting with Beast7m 42s
-
Baking the game scene4m 20s
-
Creating lighting effects6m 24s
-
Adding a flashlight9m 20s
-
-
10. Keyframing Animation
-
Using the Animation view9m 53s
-
Animating transparency7m 12s
-
Keyframing events6m 28s
-
Avoiding keyframing pitfalls5m 27s
-
11. Animating Skinned Meshes and Controlling Characters
-
Importing skinned meshes9m 42s
-
Dynamic parenting6m 44s
-
-
12. Working with Cameras and Layers
-
Introducing cameras4m 53s
-
Managing multiple levels7m 34s
-
-
13. Creating Game GUIs
-
Working with the Unity GUI10m 31s
-
Using GUI skins6m 30s
-
Introducing fonts and styles5m 56s
-
Scripting in-game menus3m 44s
-
Hooking up an in-game menu5m 20s
-
Creating custom cursors8m 42s
-
-
14. Extra Techniques and Features
-
Creating fade transitions5m 34s
-
Setting AI pathfinding9m 56s
-
-
Conclusion
-
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.
CancelTake 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.
Share this video
Embed this video
Video: Adding obstacles and pushing physics