Join Sue Blackman for an in-depth discussion in this video Working with physics components, part of Unity 3D: 3.5 Essential Training.
So what if you wanted a sign outside an old inn to swing in the wind during the whole game? First off, you'd probably only have it swing when the player was around to see it. You already know a few ways to do that. Assuming we'd eventually make it dependent on the player's presence, we would still need to know how to keep it swinging, and that's where physics--the Constant Force component--comes into play. Let's add one to our board parent. That's where all the action is. Remember, the board is just going along for the ride.
So we will select our board parent, go to Component > Physics, and way down at the bottom, we will find Constant Force. In the Inspector, we can see that we've got Force and Relative Force. The first is global coordinates. So if you had a bunch of physics objects that were going to be affected by wind, you'd use this one. Relative would be useful for pushing an object the direction it was facing.
We're going to use Force. Let's open it up. If I check my scene gizmo, it looks like I need to use the Z direction. Positive values go one direction and negative ones go the opposite. In our Scene view, our Z is on the right-hand side, but we need our wind to blow to the left, so we're going to need to use a negative value in our Z parameter. So back in the Inspector, in the Constant Force component, I'm going to type in a -1.
Before we hit play, let's go ahead and fix one thing. Our cube falls, so in the Inspector, in the Rigidbody, let's tell that guy to be Is Kinematic. Now we are ready to click play and check on our wind. So let's select our board parent again and expose the Constant Force parameters in the Inspector and hit Play. Now you can see our board swinging. You might want to try increasing the force to see what reaction we get.
Okay, let's go ahead and exit play mode. The next parameter in the Constant Force is Torque. Torque is rotation. We're going to go ahead and add a Constant Force to the original cube, Cube1, and we'll try Torque on it. So Component > Physics > Constant Force. This time we didn't add a Rigidbody first, but because we added Constant Force, one was added for us.
And let's set the Torque, the Y parameter-- since that's up in the world--to about 50. Now there's one more thing we need to know. According to the docs, never have transforms and physics controlling an object at the same time. That means we need to disable the rotation script on this cube before we test. And we can just turn it off or we could right-click and say Remove Component.
Now we'll click play, and the original cube is happily rotating. If we left it going for a while, it might wander off the board, so we're going to try something else. Exit play and to keep him from leaving town, we need to go up to his Rigidbody, open Constraints, and now we can freeze his position to keep him from going anywhere but spinning around.
In case you're wondering, when a character is turned into a rag doll, all the objects were animated by keyframe animation and they already had Rigidbodies set to Is Kinematic. They're unchecked dynamically and the keyframe animation turned off to allow the character to tumble into a heap of bones. Okay, since they're pretty important, let's do a little recap on physics rules. Number one: objects that have colliders and are transformed by other than physics need to have a Rigidbody with Is Kinematic checked.
Two: don't mix active physics and transforms at the same time. Three: don't have parent and child Rigidbodies together and use joints to create hierarchies. And four: never scale the parent of a Rigidbody. Remember, children inherit the transforms of their parents.
- 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?
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.