Download

OnOverlap not working,

Hi, I am programming a space shooter, and the collision between the bullet and the enemy ship is not working, here is the code
I know there is a typo on the name (Bulllet) but it happened when I created the class and I do not know how to change it from unreal, I tried refactoring via
VS but to no avail

https://pastebin.com/0EevyWVQ

BullletController.h




// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Engine/Classes/Components/ShapeComponent.h"
#include "BullletController.generated.h"


UCLASS()
class MYGAME_API ABullletController : public AActor
{
    GENERATED_BODY()

public:    
    // Sets default values for this actor's properties
    ABullletController();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    UPROPERTY(EditAnywhere)
        UShapeComponent* collisionBox;

    UPROPERTY(EditAnywhere)
        float speed = 400.0f;

    UFUNCTION()
        void OnOverlap(UPrimitiveComponent* OverLappedComponent,
            AActor* OtherActor, UPrimitiveComponent* OtherComponent, 
            int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);




};



BullletController.cpp



#include "BullletController.h"
#include "EnemyController.h"
#include "Components/BoxComponent.h"


// Sets default values
ABullletController::ABullletController()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
    collisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
    collisionBox->bGenerateOverlapEvents = true;
    collisionBox->OnComponentBeginOverlap.AddDynamic(this, &ABullletController::OnOverlap);
}

// Called when the game starts or when spawned
void ABullletController::BeginPlay()
{
    Super::BeginPlay();

}

// Called every frame
void ABullletController::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    FVector newLocation = GetActorLocation();
    newLocation.X -= speed * DeltaTime;
    SetActorLocation(newLocation);
    if (newLocation.X < -1000.0f) {
        this->Destroy();
    }
}


void ABullletController::OnOverlap(UPrimitiveComponent* OverLappedComponent, 
    AActor* OtherActor, UPrimitiveComponent* OtherComponent, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 
{
    if (OtherActor->IsA(AEnemyController::StaticClass())) {
        this->Destroy();
        OtherActor->Destroy();
    }
}


Start by checking your collision setup. Collision Overview

This, also try move collisionBox->OnComponentBeginOverlap.AddDynamic(this, &ABullletController::OnOverlap); from constructor to BeginPlay(), sometime happen that if you call it in the constructor doesn’t bind the function.

I agree with the above sentiment for the most part - you can setup delegate bindings like that in the constructor but just be aware that they will be serialized. If you decide to make a change to the components later, any blueprints you created might break when opened.

Setting them up PostInitializeComponents can be a good place to do it (bit safer than begin play for some items), and you can also use a GetWorld()->IsGameWorld() check to ensure it doesn’t get set when opening the item in the Blueprint Editor :slight_smile: