Children of my custom USceneComponent do not use parents transform

I have found the answer to my problem in this post:

My understanding of the problem is as follows:

In the constructor of my UCameraMovementComponent I set attachement to this:

UCameraMovementComponent::UCameraMovementComponent()
{
	PrimaryComponentTick.bCanEverTick = true;
	PrimaryComponentTick.SetTickFunctionEnable(true);

	mesh_ = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	mesh_->bAutoActivate = true;
	mesh_->bAutoRegister = true;
	mesh_->SetupAttachment(this);
}

When the CDO of my class is constructed, this is pointing to the CDO and not the specific instance of my class. Since mesh_ is a UPROPERTY it will be get replaced in the actual instance after construction with the version from the CDO. This means my class instance has a mesh_ that is attached to the CDO, and not my instance.

To Solve this issue I need to reattach the mesh_ to the correct parent when the component is registered (after the CDO values are copied)

void UCameraMovementComponent::OnRegister() {
    Super::OnRegister();
    this->mesh_->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);
}

With this addition the component works as expected.

1 Like