I am trying to enable collision on a custom class, however I am having a hard time figuring out how to do it programmatically.
Source:
// Sets default values
ADestoryable::ADestoryable() :Super(), m_hp(0)
{
struct FConstructorStatics
{
ConstructorHelpers::FObjectFinderOptional<UStaticMesh> PlaneMesh;
ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> BlueMaterial;
ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> OrangeMaterial;
ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> GrayMaterial;
FConstructorStatics()
: PlaneMesh(TEXT("/Game/Puzzle/Meshes/PuzzleCube.PuzzleCube"))
, BlueMaterial(TEXT("/Engine/TemplateResources/MI_Template_BaseBlue.MI_Template_BaseBlue"))
, OrangeMaterial(TEXT("/Engine/TemplateResources/MI_Template_BaseOrange.MI_Template_BaseOrange"))
, GrayMaterial(TEXT("/Engine/TemplateResources/MI_Template_BaseGray.MI_Template_BaseGray"))
{
}
};
static FConstructorStatics ConstructorStatics;
// Create dummy root scene component
DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0"));
RootComponent = DummyRoot;
// Create static mesh component
BlockMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BlockMesh0"));
BlockMesh->SetStaticMesh(ConstructorStatics.PlaneMesh.Get());
BlockMesh->SetRelativeScale3D(FVector(0.1f, 0.1f, 0.1f));
BlockMesh->SetRelativeLocation(FVector(0.f, 0.f, 25.f));
BlockMesh->SetMaterial(0, ConstructorStatics.BlueMaterial.Get());
BlockMesh->AttachTo(DummyRoot);
// Save a pointer to the orange material
OrangeMaterial = ConstructorStatics.OrangeMaterial.Get();
// Save a pointer to the orange material
BlueMaterial = ConstructorStatics.BlueMaterial.Get();
// Save a pointer to the red material
GrayMaterial = ConstructorStatics.GrayMaterial.Get();
SetActorEnableCollision(true);
BlockMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
BlockMesh->bGenerateOverlapEvents = true;
BlockMesh->SetNotifyRigidBodyCollision(true);
BlockMesh->OnComponentBeginOverlap.AddDynamic(this, &ADestoryable::OnOverlapBegin);
}
// Called when the game starts or when spawned
void ADestoryable::BeginPlay()
{
Super::BeginPlay();
}
void ADestoryable::ChangeColorToGray()
{
BlockMesh->SetMaterial(0, GrayMaterial);
}
//Change block color to orange if object is hit
void ADestoryable::OnHit(AProjectile *p)
{
if (m_hp <= 0) return;
//Damage the target
m_hp -= p->Damage();
//if we reach 0, change the color
if (m_hp <= 0)
ChangeColorToGray();
}
void ADestoryable::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
AProjectile projectileObj = dynamic_cast<AProjectile>(OtherActor);
if (!projectileObj) return;
OnHit(projectileObj);
projectileObj->Destroy();
}
Header:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "Destoryable.generated.h"
class AProjectile;
UCLASS()
class MISSILEDEFENDER_API ADestoryable : public AActor
{
GENERATED_BODY()
/** Dummy root component */
UPROPERTY(Category = Block, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class USceneComponent* DummyRoot;
/** StaticMesh component for the clickable block */
UPROPERTY(Category = Block, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* BlockMesh;
UStaticMesh* BlockM;
protected:
int m_hp;
public:
/** Pointer to orange material used on active blocks */
UPROPERTY()
class UMaterialInstance* OrangeMaterial;
/** Pointer to blue material used on active blocks */
UPROPERTY()
class UMaterialInstance* BlueMaterial;
/** Pointer to red material used on active blocks */
UPROPERTY()
class UMaterialInstance* GrayMaterial;
// Sets default values for this actor's properties
ADestoryable();
/** called when projectile hits something */
UFUNCTION()
void OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void ChangeColorToBlue();
void ChangeColorToOrange();
void ChangeColorToGray();
virtual void SetHP(int hp) { m_hp = hp; }
virtual void OnHit(AProjectile *p);
};
For some reason, when I have two “Destoryable” objects collide, it does not trigger the OnOverlapBegin event and I’m not really sure why. Does this have to do with the fact that I might be enabling collision on the wrong component?