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.

  1. Select Player in the Hierarchy panel.
  2. At the bottom of the Inspector panel,navigate to Add Component | Physics | Rigidbody.
  3. In the Rigidbody Component, check Is Kinematic.

The following screenshot shows the code for moving Player using physics forces:

Time for action – adding a Rigidbody to the Player

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.

What just happened?

An analysis of the code we saw in the preceding code screenshot is as follows:

Note that FixedUpdate() is being used directly in PlayerControl instead of being controlled by the State Machine:

  • The State Machine is controlling the isKinematic setting
  • When isKinematic is checked, physics code does not work to move Player so there's no need to have the State Machine controlling FixedUpdate()

Line 7: public floa t speed = 16.0f;

  • This variable will be used as a multiplier to make the forward and backward speed of Player faster

Line 8: public float rotationSpeed = 0.60f;

  • This variable will be used as a multiplier to make Player rotate faster

Line 27: fl oat foreAndAft = Input.GetAxis("Ver tical") * speed;

  • The Input.GetAxis("Vertical") method detects the pressing of the Up Arrow or Down Arrow keys
  • The up arrow key returns a value of 1, while the down arrow key returns a value of -1
  • These values are multiplied by speed which contains the value 16.0
  • The result, 16 or -16, is stored in the variable foreAndAft

Line 28: float rotation = Input.GetAxis("Horizontal") * rotationSpeed;

  • Input.GetAxis("Horizontal") method detects the right or left arrow keys
  • The right arrow key returns a value of 1, the left arrow key returns a value of -1
  • These values are multiplied by rotationSpeed which contains the value 0.60
  • The result, 0.60 or -0.60, is stored in the variable rotation

Line 29: rigidbody.AddRelativeForce (0, 0, foreAndAft);

  • Since we're using the Rigidbody class to move Player, the Scripting Reference provides AddForce() and AddRelativeForce() for moving objects
  • AddForce() makes objects move along the x, y, and z axes of the world, which isn't so good if Player isn't facing the direction where the force is being applied
  • AddRelativeForce() makes an object move according to it's own x, y, and z axes, so applying force to the z axis of Player moves it correctly

Line 30: rigidbody.AddTorque (0, rotation, 0);

  • This allows Player to rotate or turn

Line 33: void OnTriggerStay(Collider other)

  • Unity automatically calls this method when a collider of another GameObject is inside Box Collider of Player

Line 35: rigidbody.AddForce(Vector3.up * hoverPower);

  • In Scripting Reference, this is one of two examples to apply upward force
  • This could have been written rigidbody.AddForce(0, hoverPower, 0);
  • When the Terrain is inside Box Collider of Player, upward force pushes Player up
  • As Player rises higher, Terrain gets removed from Box Collider of Player
  • This removes the upward force and Player lowers, causing Terrain to be detected
  • This gives Player the effect of hovering over the Terrain
..................Content has been hidden....................

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