Parenting sprites

Inverse kinematics is a technique consisting of placing joints between elements that move together so that the movement of one element influences the other elements it is attached to. Our body, for example, is a collection of parts connected with joints that move according to rules of inverse kinematics. When we raise an arm, the arm, forearm and the hand, as well as the individual fingers, all follow according to their joints.

By setting the pivot points on the sprite chunks at the end of the previous recipe, we actually defined the joints to build the inverse kinematics for the character. Now we will parent the pieces together by their pivot points so that we can finally animate the character.

Getting ready

We keep up from where we left, so have your project open and be ready to follow our instructions.

How to do it...

  1. Let's start by creating new Empty GameObject in the scene and naming it Character.
  2. Select all pieces of the knight (six in total) and drag them onto Character in the scene.
  3. Unity opens a window asking us to save the animation clip we are about to create. Go to Assets | Sprites | Anims and name the clip attack, as shown in the following screenshot:
    How to do it...
  4. Uncheck Root Motion and check Animate Physics in the Animator panel in the Inspector window, (just as we did before) as shown in this screenshot (if you don't remember what root motion is, you can check it out in Chapter 1, Importing 3D Models and Animations):
    How to do it...
  5. Now we can actually parent the pieces belonging to the knight. All the chunks should be in the scene by now. If they are not, drag them one at a time into the scene.
  6. Let's start by parenting Body to Character. Select Body in the Hierarchy window and drag it onto Character.
  7. Body will be our reference for all other pieces. Now drag Head onto Body.
  8. Then drag Arm_L and Arm_R onto body.
  9. Finally, drag Foot_L and Foot_R similarly. The following screenshot shows Hierarchy you should see in your own editor:
    How to do it...
  10. If you check out your character in the editor scene or the game scene, you may notice that the body parts of the knight actually overlap and produce a few perspective glitches. As we learned in the recipe titled Setting Up Sprites of this chapter, when a sprite is instantiated in the game scene, Unity automatically provides it with a Sprite Renderer component. By inspecting the Sprite Render panel in the Inspector window, you should see two properties we didn't mention so far: Sorting Layer and Order in Layer.
  11. Sorting Layer defines the order in which the sprites are rendered on screen on a macro scale, so to say. You can use layers to send all background elements of a scene to the back so that animated sprites can be drawn above them. To create a new sorting layer, click on the drop-down menu in the panel (which should be on default right now) and select Add Sorting Layer..., as shown in the following screenshot:
    How to do it...
  12. Order in Layer defines the order inside a layer created by the user: the higher the number, the closer the sprite to the eyes of the player.
  13. In our case, we could assign indexes as follows: 0 to Body, 1 to Head and Foot_R, 2 to Arm_R, and -1 to Arm_L and Foot_L. Here is a screenshot that shows the result we should get:
    How to do it...

How it works...

Parenting game objects in Unity is really easy; all it takes is to drag one object onto another in the Hierarchy panel. Once two or more objects are parented together, you can move, rotate, or scale the entire group by manipulating the highest element in the hierarchy, but you can also manipulate the individual child objects by selecting them. The Sprite Renderer component also allows you to set single sprites from the same image in the correct rendering order by defining positive or negative indexes.

With the pivot points set and the parts correctly parented together, we are ready to finally animate the knight, which we will cover in our next recipe.

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

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