Hi!
I’m trying to attach a component to another in C++. It successfully attaches but the Position doesn’t change when I set : FAttachmentTransformRules::KeepRelativeTransform in the AttachToComponent function.
For example, if my object is in (0, 0, 0) world position, the object I attach it to is at (10, 0, 10), the object position after attachment will me (-10, 0, -10) (as its world position will not have changed)
/* the BeginPlay of PlayerPawn the element I want to attach to the GameBox */
void APlayerPawn::BeginPlay()
{
Super::BeginPlay();
// I put the world transform to (0, 0, 0) just to show that the relative transform doesn't work
RootComponent->SetWorldTransform(FTransform());
auto World = GetWorld();
for (TActorIterator<AGameBox> It(World); It; ++It)
It->AttachComponent(RootComponent);
}
/* declaration of AttachComponent in GameBox.h */
void AttachComponent(USceneComponent* c,
const FAttachmentTransformRules& AttachRules = FAttachmentTransformRules::KeepRelativeTransform);
/* Defininition of AttachComponent in Gamebox.cpp (Plane is the RootComponent of GameBox */
void AGameBox::AttachComponent(USceneComponent* component,
const FAttachmentTransformRules& attach_rules)
{
component->AttachToComponent(Plane, attach_rules);
}
I have tried not using the default argument for AttachComponent but it doesn’t change anything.
The code is available here:
(it’s very simple so should be easy to understand)