The original tutorial is based on UE 4.18, I am based on UE 4.25.

English original Address

Following the tutorial, we continue declaring the OnOverlapBegin and OnOverlapEnd methods in the public section of the character.h file. My header file in this tutorial is called unrealCppcharacter.h, but your file may have a different name. You can learn more about OnComponentBeginOverlap and OnComponentEndOverlap here.

public.// declare overlap begin function
	UFUNCTION(a)void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	// declare overlap end function
	UFUNCTION(a)void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
Copy the code

In this tutorial, we will add a capsule component for the character to handle triggering events specifically.

class AUnrealCPPCharacter : public ACharacter
{
  GENERATED_BODY()

  ...

	// create trigger capsule
	UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule")
  class UCapsuleComponent* TriggerCapsule;
Copy the code

We have completed the header file. Move on to the.cpp file.

If you haven’t contain Components/CapsuleComponent. J h, at the top, please include this file.

#include "Components/CapsuleComponent.h"
Copy the code

In the constructor of Character, we will add the capsule component to it and wire it to the overlapping event.

To add a capsule to our character, we first create a UCapsuleComponent component through CreateDefaultSubobject and name it whatever we want. I call it the Trigger Capsule.

Next, we have to initialize its size, and I make it the same size as the initial capsule component (declared in the constructor). We can also set the collision type for this component using SetCollisionProfileName. We add the Trigger Profile file name to the capsule, which provides the component with overlapping events similar to the Trigger capsule. This can also be easily set up in the editor. Finally, attach the TriggerCapsule to the RootComponent

AUnrealCPPCharacter::AUnrealCPPCharacter() {...// declare trigger capsule
	TriggerCapsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
	TriggerCapsule->InitCapsuleSize(55.f.96.0 f);;
	TriggerCapsule->SetCollisionProfileName(TEXT("Trigger"));
  TriggerCapsule->SetupAttachment(RootComponent);
}
Copy the code

We connect capsules to overlapping events by calling OnComponentBeginOverlap and OnComponentEndOverlap.

.void AUnrealCPPCharacter::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if(OtherActor && (OtherActor ! =this) && OtherComp) 
	{
		GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Red, TEXT("Overlap Begin")); }}void AUnrealCPPCharacter::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if(OtherActor && (OtherActor ! =this) && OtherComp) 
	{
		GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Red, TEXT("Overlap End")); }}Copy the code

We manually added a Triggger Box to the scene

The control Character then overlaps with it, and here is the final result