Damaging with Objects in 3D Game Kit

확인 완료한 버전: 2018.1

-

난이도: 초급

We can inflict damage on objects that has a Damageable Component using a Contact Damager.

We can Demonstrate how this works using a box and the Chomper

  • In the Project Window go to Prefabs > Interactables

  • Find the DestructibleBox Prefab and click and drag it into the Scene View

  • Use the Translate Tool to position the box above Chomper

    • Place it high up so you have time to view it in testing

  • Press Play

The box will fall and land on Chomper and stay there, nothing happens.

Note: Don’t get too close to Chomper or he’ll chase after Ellen. You can always reduce his Detection Radius to 0 if you wish.

We need the box to be causing damage when it hits Chomper.

  • In the Hierarchy click the arrow next to DestructibleBox to expose its Children

  • Select the DestructibleBox child

  • Navigate to the Inspector

  • Scroll to the bottom of the Inspector and Click Add Component

  • Search for Box Collider

  • Hit Enter on your Keyboard or Click on the Box Collider Component to add it to the DestructibleBox

  • On the Box Collider Component tick the Is Trigger box

  • Click Add Component

  • Search for Contact Damager

  • Hit Enter on your Keyboard or Click on the Contact Damager Script to add it as a Component

  • In the Contact Damager Component set the Amount to 3

    • Note that the Damage amount must be equal to the Max Hit Points you set for Chomper in order for him to receive the full amount of damage. We changed it to 3 in our project, ensure you check the Max Hit Points on Chomper’s Damageable Component
  • Set the Damaged Layers to Enemy

  • Press Play

The box will land on Chomper, it will lose its health, turn into its ragdoll state and the box will bounce away. We have successfully caused enough damage to kill the Chomper, but this effect doesn’t look great. We can make the box break when it hits Chomper. It already has a Contact Damager, so let’s adapt it to affect the box.

  • In the Hierarchy

  • Find Chomper and select it

  • Navigate to the Inspector

  • Scroll to the bottom of the Inspector

  • Click Add Component and search for Contact Damager

  • Hit Enter on the Keyboard or click on the Script to add it to Chomper

  • In the Contact Damager Component

  • Set the Amount to 1

  • In the Damaged Layer drop down select Enemy

  • Press Play

The box will fall on Chomper, break on impact and make Chomper lose all health and ragdoll.