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.
Clean the game scene of any former character and add a new one. Follow the recipe to know what to do next.
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.
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.The following screenshot shows the settings to use a Rigidbody in our platform game:
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.
3.16.76.138