Begin Overlap/End Overlap firing

Originally posted in UE4 Answers

Hi everyone,

I have a custom actor that I am wanting to interact with when I am next to it, like for a door. I created an overlay sphere component but both my begin and end overlap functions are being fired off at the same time, and when I exit the overlay just my end overlap function fires. I set them all up initially in C++; I did play around with them in the blueprints but I haven’t had any luck. Am I missing something? Here is the code I am using in my constructor to set everything up and the parent class of my object is an actor:


//Create RootComponent
     sceneComponent = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("SceneComponent"));
     RootComponent = sceneComponent;
 
     overlaySphereComponent = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("OverlaySphereComponent"));
     overlaySphereComponent->SetSphereRadius(250.0f);
     overlaySphereComponent->AttachParent = RootComponent;
     overlaySphereComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
     overlaySphereComponent->SetCollisionResponseToChannel(ECC_Pawn, ECollisionResponse::ECR_Overlap);
 
     overlaySphereComponent->OnComponentBeginOverlap.AddDynamic(this, &ABaseInteractObject::OnWorldOverlapBegin);
     overlaySphereComponent->OnComponentEndOverlap.AddDynamic(this, &ABaseInteractObject::OnWorldOverlapEnd);

I am using 4.7. Is anyone able to shed some light or did I go about this wrong?

Thanks everyone!

So it turns out there is a difference between Character and Pawn/Actor that is causing this. I tested it on one of my custom character classes and it fired properly, but not when the parent class is a pawn/actor. That makes me think that it is something within the actor vs character or how the root component is setup as I am creating the root component myself for this actor.

Take your class, and make a Blueprint extend off of it. Go into the blueprint, select your SphereComponent and look at the Collision settings. Chances are you need to setup your Collision profile.



overlaySphereComponent->SetCollisionProfileName("OverlapAll");

Hi IrishKilter,

I tried messing around with the collission profile in C++ and in the blueprint settings and still having the same issue. The above code that I have (excluding the root component creation) is the same code I put in my custom npc character class and the events fired fine on the npc character.

I found out yesterday that if I make a blueprint off my base interact object,it does fire the events properly (yay!) but the child class it isn’t, which is what I’ve been testing with initially. So now to figure that out. I am calling super in my constructor of the child class.

How are you calling super? Constructors don’t use the traditional Super::FunctionName() that functions do. (Or at least as far as I’m aware they don’t).

Instead the Super is apart of the Constructor “declaration” inside your CPP file.

IE:


ABRBeachHead::ABRBeachHead(const FObjectInitializer& ObjectInitializer)
**:Super(ObjectInitializer)**

Also, I believe this:


     overlaySphereComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
     overlaySphereComponent->SetCollisionResponseToChannel(ECC_Pawn, ECollisionResponse::ECR_Overlap);

is telling your object to ignore everything except for objects with their Collision set to “Pawn”, which is basically just Pawns/Characters. I’m not sure if that’s intended or not.

As far as overlapping events, a few things to consider. Whenever you setup collision, BOTH the Collidee and the Collided objects need to have “harmonious” setups. This means that both objects need to have their “Response channel” set to Overlap with each other. So if you have Object A set to “Ignore All” and Object B to “Overlap All” nothing will happen, because one of the two objects does not specificy to “Overlap”. Another example, is if you have Object A set to Overlap with Pawns, Geometry, Static Meshes, and Dynamic Objects but you have Object B to Overlap with Geometry, Static Meshes, Dynamic Objects but IGNORE Pawns. If Object A has their Collision Channel set to Pawn nothing will happen even though the pawn is told to cause an overlap event.

On top of that, BOTH objects need to have bGenerateOverlapEvents set to True to work. It’s all very confusing.

This is all I needed to setup collision on a Projectile I made after seeing your post using version 4.8.



OnActorBeginOverlap.AddDynamic(this, &ABRProjectile::BeginOverlap);

	CollisionComponent = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("CollisionComp"));
	RootComponent = CollisionComponent;

	CollisionComponent->SetCollisionProfileName("OverlapAll");
	CollisionComponent->InitSphereRadius(48.0f);
	CollisionComponent->bGenerateOverlapEvents = true;


If your base class works, and the child doesn’t, something is being overridden, or the object you’re colliding into isn’t setup to call Overlap Events for your class properly.