Animating your own characters with the Microsoft Kinect controller

Perhaps the best way to understand how the Zigfu objects and event messages can be used in your own Unity projects is to build a basic figure out of cubes, and associate particular cubes with particular skeleton objects. In this recipe, you'll build part of a block character from cubes (a head, left shoulder, elbow, and hand), and use Zigfu scripts to be able to control this arm when the game runs with the Kinect controller attached.

Getting ready

Please note that the scripts you'll be using in this recipe can be found in the following folders of the Project panel once you have imported the ZDK Unity package:

  • Zigfu/Scripts/UserEngagers/
  • Zigfu/Scripts/UserControls/
  • Zigfu/SampleScenes/Scripts/
  • Standard Assets/Scripts/CameraScripts/

How to do it...

  1. Create a new Unity project and import the ZDK package.
  2. Add Directional Light.
  3. Create an empty game object and name it MyContainer.
  4. Create an empty game object named MyCharacter, and in the Hierarchy view, drag this object to become a child of MyContainer.
  5. Create the following cubes, and in the Hierarchy view, drag these objects to become a children of MyCharacter:
    • A cube named CubeHead at (0,0,0) sized (0.2, 0.2, 0.2)
    • A cube named CubeShoulderL at (0.2, -0.2, 0) sized (0.2, 0.2, 0.2)
    • A cube named CubeElbowL at (0.5, -0.2, 0) sized (0.2, 0.2, 0.2)
    • A cube named CubeHandL at (0.8, -0.2, 0) sized (0.2, 0.2, 0.2)

    Note

    The positions of these cubes don't matter since their positions in the game will be controlled by the head/arm positions detected by the Kinect sensor. However, it can be handy to lay them out in an approximation of a block-person to avoid getting confused when adding new cubes.

  6. Create an empty game object and name it Zig:
    How to do it...
  7. Add to Main Camera the SmoothFollow scripted component, set its Distance variable to 4, and with Main Camera selected, in the Inspector view drag CubeHead into the public variable slot for Target for the SmoothFollow scripted component.
  8. Add the Zig scripted component to Zig.
  9. Add ZigEngageSingleUser scripted component to Zig.
  10. While ensuring Zig is selected, in the Inspector view for the ZigEngageSingleUser component drag MyCubeCharacter over the Engaged Users public variable (its size should increase from 0 to 1, and MyCharacter should be listed as Element 0).
  11. Add the ZigSkeleton scripted component to MyCharacter. In the Inspector view, ensure the following options are ticked (checked):
    • Update Joint Positions
    • Update Root Positions
    • Update Orientation
  12. With MyCharacter selected, drag the following cubes in the Inspector view:
    • CubeHead into the public variable slot for Head
    • CubeShoulderL into the public variable slot for Left Shoulder
    • CubeElbowL into the public variable slot for Left Elbow
    • CubeHandL into the public variable slot for Left Hand
  13. Run the scene. You should now be controlling the cubes on screen as you move your head and left arm!

How it works...

The ZigEngageSingleUser component applies observed (Kinect input) positions and rotations for skeleton objects to associated game objects in the scene. In step 12, you linked the transforms of the cubes you created with particular skeleton elements that the ZigEngageSingleUser component is tracking form the Kinect. For each frame, this component applies the position and transform changes to your cubes, and we see our block character move as we move ourselves in front of the camera.

There's more...

Here are some details you don't want to miss.

Completing your block character

To complete your block character, you might use a capsule game object for the torso, and cubes for the other parts of the arms and legs. You might wish to refer to the Zigfu Blockman3rdPerson sample scene, since it is essentially the same as this recipe when it has been completed.

See also

  • The Control characters in Unity with the Microsoft Kinect using the Zigfu samples recipe.
  • The Homemade mocap by storing movements from the Microsoft Kinect controller recipe.
..................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