skip to Main Content

Generating Hit Events

Unreal Engine 4 provides an easy way to generate Hit Events. We have two different types of Hit Events:

  • OnHit event, which fires when the actor hits something
  • Receive Hit event, which fires when the actor gets hit by something

Let’s see how we can use these functions.

Creating a Falling Actor

In this post, I’m going to create two cubes and place them in mid-air. When the first cube touches the ground the OnHit event will fire and when the second cube touches the first cube, the receive hit event will fire from the cube on the ground and the on hit event will fire from the cube that just fell.

To create the above behavior, create a C++ class which inherits from Actor class and in the header file, type in the following:

The OnHit and ReceiveHit have these parameters because later on we need to register a specific function to fire. When hooking up functions that fire when something happens, you must provide a function with a specific signature. For this case, these functions will work since their signatures are provided by Epic.

Once you type in the above code, switch to the source file. We need to:

  1. Register that we want Hit events to fire
  2. Implement the OnHit function
  3. Implement the ReceiveHit function

The first step is done inside the constructor of the FallingActor. All the logic described above is provided in the following code:

Compile your code, and create a Blueprint based on the C++ class we created above. Then, make sure you set up the marked properties similar to the image below:

hit_bp

Once you’re done with that, switch to your viewport and place this actor somewhere above the ground, then, duplicate the actor and place the copy right above the first one like the image suggests:

falling_actors

Now, if you click play or simulate, the moment the first cube touches the ground the OnHit event will constantly spam your log because your cube will be touching the ground and when the second cube lands in the first one, the ReceiveHit event will fire. After that, both cubes will be spamming your log with the OnHit events.

Avatar photo

This Post Has 2 Comments

  1. Thanks for the snippet!

    UPGRADE NOTE

    Make sure to override NotifyHit instead of ReceiveHit on Unreal 4.8+
    https://forums.unrealengine.com/showthread.php?69212-AActor-ReceiveHit-in-4-8

    I’d also recommend to call Super::NotifyHit() too since the base implementation actually calls ReceiveHit (which is not virtual anymore), which itself is BlueprintImplementable, so that if a designer wants to add a Blueprint behaviour on top of your C++ implementation, he/she can.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top