Bouncy balls

In Unity, the physics behavior of an object is defined separately from its shape (mesh) and renderer (material). The Unity physics engine controls the positional and rotational forces that affect the transform of an object, such as gravity, friction, momentum, and collisions with other rigid bodies. The items that play into physics include:

  • Rigidbody component
  • Collider component
  • Physic material
  • Project Physics Manager

So, two things will be needed to make our ball bounce. It must have a Rigidbody component, so the Unity physics engine will know to apply forces to it. In particular, we want the ball to respond to gravity. Secondly, we will apply a physic material so the ball not only falls, but then bounces when it hits another object.

To add a Rigidbody:

  1. Select the Ball in Hierarchy, and Add Component | Rigid Body.
  2. Check its Use Gravity checkbox.

If you press Play now, you will see the ball drop like a brick. It hits the floor and just stops. Let's make it bouncy by applying a physics material (Unity spells it without the "s").

  1. In Project Assets/ARPlayBall/PhysicMaterial, create a new physic material (right-click Create | Physic Material).
  2. Name it BouncyBall.
  3. Set its Bounciness to 0.8.
  4. Set the Bounce Combine to Maximum.
  5. Drag the BouncyBall physic material onto the Ball's Sphere Collider's Material slot, as shown in the following screenshot:
Note that pressing Play while ThrowingGame is a child of ImageTarget may cause the ball to fall infinitely because the Floor plane is not instantiated until the image target is recognized, but Unity begins applying gravity to the ball immediately. We will take care of this in scripting when we throw the ball. For now, to test your bouncy ball, you can move ThrowingGame to the root of the Hierarchy. Alternatively, you can uncheck Use Gravity in the Ball's Rigidbody, press Play, find the target image so the court is instantiated, then, in the editor, check Use Gravity to watch the ball drop and bounce.

Press Play now and the ball with bounce, slowly diminishing height, and eventually comes to a stop.

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

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