Adding Rigidbody

Another approach to control a game character is to use a Rigidbody component and make it subject to physics.

The Rigidbody adds several features to make a character behave as if it were a real physical object—it tends to fall down, it is pushed by other objects, and pushes them as well. If you want your game to have a realistic feel, then a Rigidbody is what you need.

The problem is that, more often than you may think, realism doesn't necessarily mean fun and engagement. As a matter of fact, the control style dependent on a Rigidbody is pretty realistic, but it is also not very precise. Momentum, friction, weight... these are all examples of variables you need to manage and appropriately tweak to get optimal game controls. In other words, with a Rigidbody you get fluidity, but you need to be comfortable with physics and have excellent programming skills to get true control.

With regard to gravity management, a Rigidbody has a parameter called Mass that defines how strong the character is attracted by the ground based on its physics properties. Imagine you have several GameObjects in your scene, each with its own Rigidbody. Should you decide to change the physics model as a consequence of a game event, you can change the defintion of gravity in your game and your changes will affect all Rigidbodies in the scene. With a Character Controller, you couldn't do that!

Another difference is that Rigidbodies allow us to use physics materials. A Rigidbody offers the opportunity to create special surfaces where the character slides as if it were ice, or slows down, or anything you may think of. This can be pretty useful for a platform game; however, the Character Controller doesn't support it!

With this recipe, we show you how you can add a Rigidbody component to a game character and how to tweak its parameters for a platform game.

Getting ready

Clean the game scene of any former character and add a new one. Follow the recipe to know what to do next.

How to do it...

  1. Go to the Project panel and access the Prefabs folder. Now drag a new Character prefab into the scene and be sure it has no Character Controller attached to it.
  2. With Character selected in the Hierarchy, click on the Component menu item and navigate to Physics|Rigidbody from the menu, as shown in the following screenshot:
    How to do it...
  3. If you now move to the Inspector window, you can take a look at the parameters to control the behavior of the Rigidbody. Mass represents...well, the mass of a game object—how much it is subject to physics forces. Objects with high mass tend to fall down quickly, keep their momentum if they collide, and push other objects.

    An important thing to say about Mass is that it may happen that your imported models act strangely when added with a Rigidbody. The reason may be that the scale of your imported model is wrong. Always consider that in Unity, 1 unit = 1 meter, and that the physics engine works on the same exact scale.

  4. Drag is another important parameter that defines the inertia of a game object. Low values of drag mean that the object is heavy, while high values mean it is light. Checking the manual, you may learn that 0.001 means a solid block of metal, while a value of 10 corresponds to a feather. Please refer to http://docs.unity3d.com/Manual/class-Rigidbody.html for an explanation of the Drag parameter.
  5. Is Kinematic is an interesting option if you want a game object to detect collisions but don't want it to react to physics. For example, a classic 2D shooter requires collision detection but doesn't implement physics forces. Since an Rigidbody component may be necessary for detecting the collisions on a game object (check the collision detection tables at: http://docs.unity3d.com/Manual/CollidersOverview.html), the Is Kinematic option is a welcome feature of the Rigidbody component.
  6. Finally, we have a set of properties called Constraints. By flagging these properties, we decide which axis the character can move to and rotate on. For example, for a side-scrolling game, you may want the character to not move on the Z axis, and freeze its rotations on the X and Z axes.

The following screenshot shows the settings to use a Rigidbody in our platform game:

How to do it...

How it works...

A Rigidbody is one of those components you will keep making use of as you work with Unity. Game objects with Rigidbodies attached to them fall, push and can be pushed, have mass, provide a large collection of methods to interact with them, and can be fully controlled through physics. The settings we previously defined are those that meet the needs of our prototype; you may find yourself experimenting with them to find yours! With the Rigidbody component added to the character, we now have physics, but we still miss control. If we want to use physics forces interacting with the Rigidbody component to control the character, we need to script them. This is the topic of our next recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.138