Time for action – adding a Rigidbody to the Player
We're going to add a Rigidbody
Component to apply physics to Player. To stop Player from dropping like a rock, out of view of the camera, we'll check Is Kinematic in the Rigidbody properties to turn off the effect of physics. When any of the play States are active, we'll uncheck Is Kinematic in code to allow physics to affect Player.
- Select Player in the Hierarchy panel.
- At the bottom of the Inspector panel,navigate to Add Component | Physics | Rigidbody.
- In the Rigidbody Component, check Is Kinematic.
The following screenshot shows the code for moving Player using physics forces:
What just happened?
In the Inspector, the values stored in the Mass
, Drag
, and Angular Drag
variables under the Rigidbody properties affect the move ability of Player, just like in real life.
In any play State, Is Kinematic is unchecked allowing physics to work.
An analysis of the code we saw in the preceding code screenshot is as follows:
Note...