falling_actors

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.

Share postShare on Facebook0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0