How to programmatically enable gravity on a UGeometryCollectionComponent

rstojano
Sat, 14 Oct 2023 22:46:48 +0000
Programming & Scripting

I have an actor class called ABreakableActor which holds a:
UGeometryCollectionComponent* BreakableMeshComponent;

I have inherited this class with a BP that disables the gravity of the component so it just floats there. I would like to enable gravity on the object when certain events happen in the game but it seems to have no effect. For example, if I try overriding BeginPlay:

void ABreakableActor::BeginPlay()
{
	Super::BeginPlay();

	BreakableMeshComponent->SetEnableGravity(true);
}

There is no effect on the gravity of the object.

For reference here is the constructor:

ABreakableActor::ABreakableActor()
{
	PrimaryActorTick.bCanEverTick = true;

	// Initialize and set DefaultSceneRoot as the root component
	DefaultSceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("DefaultSceneRoot"));
	RootComponent = DefaultSceneRoot;

	// Create BreakableMeshComponent and attach it to the DefaultSceneRoot
	BreakableMeshComponent = CreateDefaultSubobject<UGeometryCollectionComponent>(TEXT("BreakableMeshComponent"));
	BreakableMeshComponent->SetupAttachment(DefaultSceneRoot);
}

2 posts - 1 participant