So I’m having a bit of a problem with collision detecting. very basic right? anyway I can detect an on overlap perfectly fine right. I create a bullet actor on a level and when it collides with something it destroys itself. the problem is that it now seems to destroy itself with spawned in by a player.
Anyway this is the header
UCLASS()
class TANKS2_API ABullet : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor’s properties
ABullet();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UStaticMeshComponent* body;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere)
USceneComponent* CollisionRoot;
private:
//Collsion Sphere Pointer
//USphereComponent* Collision;
UPROPERTY(EditAnywhere)
UShapeComponent * Collision;
//CollisionFunction
UFUNCTION()
void OnBeginOverlap(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};
and the cpp
ABullet::ABullet()
{
// 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;
CollisionRoot = CreateDefaultSubobject<USceneComponent>(TEXT("CollisionRoot"));
RootComponent = CollisionRoot;
//initialise the static mesh
body = CreateDefaultSubobject<UStaticMeshComponent>("body");
//this->RootComponent = body;
body->AttachTo(CollisionRoot);
//part of collision detection
this->Collision = CreateDefaultSubobject<UBoxComponent>(TEXT("ObjectName"));
this->Collision->SetGenerateOverlapEvents(true);
this->Collision->SetWorldScale3D(FVector(1.f, 1.f, 1.f));
this->Collision->OnComponentBeginOverlap.AddDynamic(this, &ABullet::OnBeginOverlap);
this->Collision->AttachToComponent(this->CollisionRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}
// Called when the game starts or when spawned
void ABullet::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABullet::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
//CollisionFunction
void ABullet::OnBeginOverlap(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
{
Destroy();
}
}