Adding rigid props to animated characters

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

Getting ready

For this recipe, we have prepared a project named MixamoProject, containing several assets such as levels, animated characters, and props. You can find it inside the 0423_05_codes folder.

How to do it...

To add a rigid prop to an animated character at runtime, perform the following steps:

  1. Open the MixamoProject project, and then open the level named 05_05 in the Levels folder.
  2. You should see Mixamo's animated S.W.A.T. soldier and two spheres. Those spheres will trigger the addition of new props for the character.
  3. In the Project view, create a new C# script named AddProp.cs.
  4. Open the script and add the following code:
    using UnityEngine;
    using System.Collections;
    
    public class AddProp : MonoBehaviour {
        public GameObject prop;
        public string propName;
        public Transform targetBone;
        public Vector3 propOffset;
        public bool destroyTrigger = true;
    
        void OnTriggerEnter ( Collider collision){
    
              if (targetBone.IsChildOf(collision.transform)){
               bool checkProp = false;
               foreach(Transform child in targetBone){ 
                   if (child.name == propName)
                        checkProp = true;
               }
    
              if(!checkProp){
               GameObject newprop;
           newprop = Instantiate(prop, targetBone.position, targetBone.rotation) as GameObject;
               newprop.name = propName;
               newprop.transform.parent = targetBone;
               newprop.transform.localPosition += propOffset;
               if(destroyTrigger)
                   Destroy(gameObject);
                  }
              }
        }
    }
  5. Save and close the script.
  6. Attach the AddProp.cs script by dragging it from the Project view onto the objects named Sphere Blue and Sphere Red in the Hierarchy view.
  7. Select each sphere, and select the Is Trigger checkbox in the Sphere Collider component (located in the Inspector view).
    How to do it...
  8. Select Sphere Blue and check out its Add Prop component.
  9. First, let's assign a Prefab to the Prop field. In the Project view, expand the Props folder. Then, drag the Prefab named Gun into the Prop field.
  10. In the Prop Name field, type in Gun.
  11. In the Target Bone field, select the swat:LeftUpLeg transform from the list (or drag it from the Hierarchy view; it is a child of the Character game object).
  12. Expand the Prop Offset field and type in the values of X, Y, and Z as -0.05, -0.15, and -0.06 respectively. Finally, deselect the Destroy Trigger checkbox.
  13. Select the Sphere Red object and fill in the variables as follows: Prop: Badge; Prop Name: Badge; Target Bone: swat:Spine2; Prop Offset: X: -0.11, Y: 0.05, Z: 0.11. Also, deselect the Destroy Trigger checkbox.
    How to do it...
  14. Play the scene. Use the WASD keyboard control scheme and direct the character to the spheres. Colliding with them will add a new handgun holster (to be attached to the character's left leg) and a badge (to be attached on the left-hand side of the character's chest).
    How to do it...

How it works...

Once it's been triggered by the character, the script attached to the spheres instantiates the assigned Prefabs, making them children of the bones they have been placed into. The Prop Offset field 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 pre-existing props of the same name before actually instantiating new ones.

There's more...

You could make a similar script to remove props. In that case, the function OnTriggerEnter would contain only the following code:

if (targetBone.IsChildOf(collision.transform)){
    foreach(Transform child in targetBone){
        if (child.name == propName)
            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
18.218.171.212