The mole

The Asset Store is full of mole assets starting at around US $ 4.00, but rather than pay for something, we will construct our own gray box asset. Our mole will be constructed of simple shapes and an animation. The mole will reside in an empty GameObject, which will serve as a container and positioning tool:

  1. Create an empty GameObject as a child of MoleGame called MolePosition (1). This item will contain our mole assets and be used to position the pesky varmint in the scene.
We add (1) to the end of any GameObject that we plan to duplicate. When Unity duplicates the object, it will increment the number accordingly.
  1. Create another empty GameObject. This one should be a child of MolePosition (1) called Mole.

With the containers in place, we can now begin constructing the mole.

  1. Add a capsule to the Mole GameObject. Rename it Body, set the position to (0, 0.17, 0) and the scale to (0.2, 0.2, 0.15). Set the material to InteractiveGrey.
  2. Lastly, give our critter some ears using two cylinders named EarLeft and EarRight, as children of Mole. Set their Transform to Rotation (-90, 0, 0) and Scale (0.05, 0.005, 0.05). For the position, try (-0.06, 0.35, 0.025) and (0.06, 0.35, 0.025). Set the materials to InteractiveGrey.

We limit our gray box object to the bare necessities. In this case, these objects are enough to illustrate a rough size and shape for our object. The addition of the ears, and reduced z-scaling will help detect any errant rotation of the otherwise symmetrical shape.

  1. Add a Capsule Collider component to the Mole GameObject. Adjust the settings to match the following values, Capsule Collider settings:
    • Center = (0, 0.17, 0)
    • Radius = 0.09
    • Height = 0.43
    • Direction = Y-Axis

If you are using a different mole object, the Capsule Collider should be sized slightly larger than the asset. It represents the zone where a hit will be registered when our mallet collider intersects with the mole collider:

  1. Save the scene.
  2. Add a Rigidbody component to the MolePosition (1) and set the value to:
    • Use Gravity = Off
    • Is Kinematic = Off
    • Interpolate = None
    • Collision Detection = Continuous Dynamic
    • Freeze Position = all values checked On
    • Freeze Rotation = all values checked On
  1. Add a new script to MolePosition (1) called MoleController. Move the MoleController script to the Scripts folder.

This script will control the mole's behavior. It will be used to determine when each mole pops up and what happens when the mallet collides with the mole's capsule collider. We will come back to define this script after building the scoring mechanism, mole animator, and animation state machine.

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

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