Adding rigid props to animated characters

In case you haven't included a sufficient number of props to your character when modeling and animating it, you might want to give her the chance of collecting new ones at runtime. In this recipe, we will learn how to instantiate a GameObject and assign it to a character, respecting the animation hierarchy.

Getting ready

For this recipe, we have prepared a Unity Package named Props, containing a basic scene that features an animated character and a prefab named badge. The package can be found inside the 1362_07_06 folder.

How to do it...

To add a rigid prop at runtime to an animated character, follow these steps:

  1. Create a new project and import the Props Unity Package. Then, from the Project view, open the mecanimPlayground level.
  2. From the Project view, add the badge prop to the scene by dragging it onto the Hierarchy view. Then, make it a child of the mixamorig:Spine2 transform (use the Hierarchy tree to navigate to MsLaser | mixamorig:Hips | mixamorig:Spine | mixamorig:Spine1 | mixamorig:Spine2). Then, make the badge object visible above the character's chest by changing its Transform Position to X: -0.08, Y: 0, Z: 0.15; and Rotation to X: 0.29, Y: 0.14, Z:-13.29.
    How to do it...
  3. Make a note of the Position and Rotation values, and delete the badge object from the scene.
  4. Add a new Cube to the scene (drop-down Create | 3D Object | Cube), rename it as PropTrigger, and change its Position to X: 0, Y: 0.5, Z: 2.
  5. From the Inspector view's Box Collider component, check the Is Trigger option.
  6. From the Project view, create a new C# Script named AddProp.cs.
  7. Open the script and add the following code:
    using UnityEngine;
    using System.Collections;
    
    public class AddProp : MonoBehaviour {
      public GameObject prop;
      public Transform targetBone;
      public Vector3 positionOffset;
      public Vector3 rotationOffset;
      public bool  destroyTrigger = true;
      
      void  OnTriggerEnter ( Collider collision  ){
        
        if (targetBone.IsChildOf(collision.transform)){
          bool  checkProp = false;
          foreach(Transform child in targetBone){
            if (child.name == prop.name)
              checkProp = true;
          }
          
          if(!checkProp){
            GameObject newprop;
            newprop = Instantiate(prop, targetBone.position, targetBone.rotation) as GameObject;
            newprop.name = prop.name;
            newprop.transform.parent = targetBone;
            newprop.transform.localPosition += positionOffset;
            newprop.transform.localEulerAngles += rotationOffset;
            if(destroyTrigger)
              Destroy(gameObject);
          }
        }
      }
    }
  8. Save and close the script.
  9. Attach the AddProp.cs script to the PropTrigger GameObject.
  10. Select the PropTrigger textbox and check out its Add Prop component. First, populate the Prop field with the badge prefab. Then, populate Target Bone with the mixamorig:Spine2 transform. Finally, assign the Position and Rotation values that we have previously made a note of to the Position Offset and Rotation Offset fields, respectively (Position Offset: X: -0.08, Y: 0, Z: 0.15; Rotation Offset: X: 0.29, Y: 0.14, Z:-13.29).
    How to do it...
  11. Play the scene. Using the 'WASD' keyboard control scheme, direct the character to the PropTrigger textbox. Colliding with it will add a badge to the character.
    How to do it...

How it works...

Once it's been triggered by the character, the script attached to PropTrigger instantiates the assigned prefab, making it a child of the bones that they have been "placed into". The Position Offset and Rotation Offset can be used to fine-tune the exact position of the prop (relative to its parent transform). As the props become parented by the bones of the animated character, they will follow and respect its hierarchy and animation. Note that the script checks for the preexisting props of the same name before actually instantiating a new one.

There's more...

You can make a similar script to remove the props. In this case, the OnTriggerEnter function will contain only the following code:

if (targetBone.IsChildOf(collision.transform)){
   foreach(Transform child in targetBone){
      if (child.name == prop.name)
        Destroy (child.gameObject);
    }
}
..................Content has been hidden....................

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