Using the OnHit Event in Blueprints

On this page we will use the OnComponentHit and Event Hit Events to apply damage to and fracture a Destructible Mesh as well as apply an impulse at the hit location to push the destructible, simulating the effects of being hit by a projectile and applying force at the hit location.

For this How To, we are using the Blueprint First Person Template project with Starter Content enabled.

  1. In your project in the Content Browser, go to the Content/Geometry/Meshes folder.

  2. Right-click on 1M_Cube Static Mesh and select Create Destructible Mesh.

    OnHit1.png

    We are creating a Destructible Mesh out of this Static Mesh so that when it is shot, we can fracture it causing it to break apart.

  3. Inside the Destructible Mesh Editor, click the Fracture Mesh button then Save and close the window.

    OnHit2.png

  4. Right-click on the newly created Destructible Mesh asset, then under Asset Actions, select Create Blueprint Using This...

    OnHit3.png

    Be sure to choose a save location and name for the new Blueprint created.

  5. Inside the Blueprint Editor for the new Blueprint, in the Components window select the Destructible component.

  6. In the Details panel, under Physics, check the Simulate Physics checkbox.

    OnHit4.png

    This Blueprint will now simulate physics.

  7. Also in the Details panel, at the bottom under Events, click the OnComponentHit Event.

    OnHit5.png

    This will add a new node to and open the Event Graph.

    OnHit6.png

  8. Left-click and drag off the Other Actor pin and search for and add the Cast To FirstPersonProjectile node.

    OnHit7.png

    Here we are Casting in Blueprints to another Blueprint called the FirstPersonProjectile Blueprint and making sure that when we hit the Destructible Mesh, that it is infact the FirstPersonProjectile Blueprint that hit it. If it is, then we can apply additional script to fracture the mesh, breaking it apart. If it is not, we will not do anything.

  9. Drag off the Hit pin and add a Break Hit Result node.

    OnHit8.png

    The Break Hit Result node gives us access to all sorts of information about the resulting hit and its information.

  10. Drag off the Hit Actor pin from the Break Hit Result and add the Apply Point Damage node.

  11. On the Apply Point Damage node, set the Base Damage to 100 and set the Damage Type Class to Damage Type.

    OnHit9.png

    The Apply Point Damage node allows us to not only specifies the amount of damage to cause, but the location at which the damage was inflicted.

  12. On the Apply Point Damage node, connect the remaining wires as shown below.

    OnHit10.png

    With all wires connected, your graph should look similar to above.

  13. Compile and Save then, close the Blueprint.

  14. Drag the Blueprint into the level from the Content Browser.

    OnHit11.png

  15. Click Play to play in the editor and use the Left-mouse button to fire a projectile at the cube.

    When you play in the editor, you will see that when you hit the cube with the projectile that is fired it causes the cube to take damage and break apart as well as applies an impulse at the location in which it was hit causing it to fly in that direction. The amount of force that is applied is defined inside the FirstPersonProjectile Blueprint which uses the Event Hit node to determine when the projectile actually hits something.

  16. In the Content/FirstPersonBP/Blueprints folder, open the FirstPersonProjectile Blueprint.

    Click the image for a larger view.

    The script in this Blueprint, checks to see if the object that is hit is simulating physics (which we set to true on our cube Blueprint). If it is, it then applies an Impulse at the location it hits (the amount is defined inside the green box which we have reduced to 50). You can adjust this value to increase/decrease the amount of impulse applied when a hit occurs.