Because I’m trying to create a overlap event in code and it won’t work with a UstaticMeshComponent attatched. I used the exact same code for a different actor but had a collision capsule instead. What has changed that stops the collision from occurring ?
Code:
Brick.cpp
Brick.cpp
#include "Mario3D.h"
#include "Brick.h"
#include "Coin.h"
#include "Engine.h"
ABrick::ABrick(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
//Brick Mesh
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));
BrickMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("BrickMeshComponent"));
BrickMesh->SetStaticMesh(Mesh.Object);
BrickMesh->SetWorldScale3D(FVector(0.64, 0.64, 0.64));
RootComponent = CollisionComp;
// Creates Collision Component
CollisionComp = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("CollisionComponent"));
CollisionComp->OnComponentBeginOverlap.AddDynamic(this, &ABrick::OnOverlap);
CollisionComp->SetBoxExtent(FVector(32, 32, 1));
CollisionComp->bGenerateOverlapEvents = true;
UPrimitiveComponent::SetNotifyRigidBodyCollision(true);
}
void ABrick::OnOverlap(class AActor * OtherActor, class UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
// Spawn Actor & Play Sound
ACoin* SpawnedActor2 = GetWorld()->SpawnActor<ACoin>(GetActorLocation() + FVector(0, 0, 300), FRotator(0, 0, 0));;
}
}
Brick.h
#pragma once
#include "GameFramework/Actor.h"
#include "Brick.generated.h"
/**
*
*/
UCLASS()
class MARIO3D_API ABrick : public AActor
{
GENERATED_UCLASS_BODY()
UPROPERTY(VisibleAnywhere, Category = Brick)
TSubobjectPtr<UBoxComponent> CollisionComp;
UPROPERTY(VisibleAnywhere, Category = Brick)
TSubobjectPtr<UStaticMeshComponent> BrickMesh;
UFUNCTION()
void OnOverlap(class AActor * OtherActor, class UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
};
Coin.h
#pragma once
#include "GameFramework/Actor.h"
#include "CoinSound.h"
#include "Coin.generated.h"
/**
*
*/
UCLASS()
class MARIO3D_API ACoin : public AActor
{
GENERATED_UCLASS_BODY()
/*Declares Collision Component*/
UPROPERTY(VisibleDefaultsOnly, Category = Coin)
TSubobjectPtr<USphereComponent> CollisionComponent;
UPROPERTY(VisibleDefaultsOnly, Category = Coin)
float RotationRate;
UPROPERTY(VisibleDefaultsOnly, Category = CoinSound)
TSubclassOf<ACoinSound> ASound;
virtual void Tick(float DeltaTime) override;
/** called when coin hits something or vice versa */
UFUNCTION()
void OnOverlap(class AActor * OtherActor, class UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
};