How to do it...

To be able to detect hits for different body parts, follow these steps:

  1. Import your character with all the necessary animations to Unity. Place it on the scene.
  1. Create a new Layer and name it BodyParts. To do so, go to the Layers menu and choose the Edit Layers option, as shown in the following screenshot:
Adding a new Layer
  1. Create a new script and name it BodyPart.cs. Create only one public string bodyPartName variable in this script. We will use it as a dummy script for our body parts. You will be able to implement custom effects of given body part hits in this script layer.
  2. Create an empty game object and name it Head.
  3. Assign the BodyPart.cs script to the Head game object. Type Head in the Body Part Name field of the BodyPart.cs component.
  4. Assign a Capsule Collider component to the Head game object.
  5. Set the Head Layer to BodyParts.
  6. Add a Rigidbody component to the Head game object and set it to Is Kinematic.
  1. Go to the EditProject Settings | Physics menu. Unselect the collisions between BodyParts <-> BodyParts Layers and BodyParts <-> Default Layers, as shown in the following screenshot :
Collision matrix for BodyParts layer
  1. Find the head bone in the character rig's hierarchy. Drag and drop the Head game object onto the head bone to parent it.
  2. Zero out the Head transform's rotation and position.
  3. Adjust the Capsule Collider's settings to roughly encapsulate the character's head shape.
  4. Copy the Head game object and name it Chest. Type Chest in the Body Part Name of the BodyPart.cs component.
  5. Drag and drop the Chest game object onto the chest bone in the character rig's hierarchy.
  6. Zero out the Chest's position and rotation.
  7. Adjust the Chest's Capsule Collider properties to roughly match the shape of the character's chest.
  1. Repeat the steps 13 to 16 for all the body parts. In this example, we've created HeadChestArmL (and a copy of it for the forearm), ArmR (and a copy of it for the forearm), LegL (and a copy of it for the shin), LegR (and a copy of it for the shin), and Groin. See the following screenshot for reference:
Body parts encapsulated by colliders
  1. All the body parts will move along with the character. They do not collide with one another or with the Default layer. It prevents the character's physics from behaving in strange ways. You may also need to turn the collisions off for other layers in your game.
  2. We are going to detect the body part hit with a simple script attached to the camera. Create a new script and name it Shoot.cs. In this script's Update() function, we constantly cast a ray from the camera's forward axis. We use the Physics.Raycast() implementation with the LayerMask parameter. This allows us to hit only those colliders that have the appropriate layer. If we hit such collider, we check if it has the BodyPart.cs component attached. If so, we set an on-screen UI text to the name of the body part we've hit:
        Transform cameraTransform = Camera.main.transform; 
 
        if (Physics.Raycast(cameraTransform.position, 
        cameraTransform.forward, out hit, maxDistance, 
        bodyPartsLayer)) 
        { 
            BodyPart p = 
            hit.collider.gameObject.GetComponent<BodyPart>(); 
            if (p != null) 
            { 
                bodyPartText.text = "Hit body part: " + 
               p.bodyPartName; 
            } 
            else 
            { 
                bodyPartText.text = "No body part was hit"; 
            } 
        } 
        else 
        { 
            bodyPartText.text = "No body part was hit"; 
        } 
  1. Attach the script to the camera.
  2. Create a UI Text in the scene and attach it to the BodyPart.cs component's Body Part Text field.
  3. Set the Body Parts Layer layer mask in the BodyPart.cs component to BodyParts.
  4. Attach any free look script to the camera; you can also use the CameraLook.cs script from the Shared Scripts folder.
  5. Play the game to see the effect.
..................Content has been hidden....................

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