I am trying to attach an actor to a component of the player but it is not working… Any suggestions?
void ABasePlayer::PickupItem(APickupItem * const item)
{
//Call the on pickup method of the item. This is called before the attachment to component method is because
//if physics are being simulated, then the physics object loses its parents.
item->OnPickupItem();
//Attach item to items component.
item->AttachToComponent(equippedItemComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale, NAME_None);
}
I don’t understand
//if physics are being simulated, then the physics object loses its parents.
you should disable physics before attaching to the other component.
other question would be
equippedItemComponent is definitely not a nullptr?
Physics sim is stopped when the OnPickupItem() method is called.
void APickupItem::OnPickupItem()
{
staticMesh->SetSimulatePhysics(false);
staticMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
}
Also equippedItemComponent is not null since before this function is called, it is initialized in BeginPlay
equippedItemComponent = CreateDefaultSubobject<USceneComponent>(TEXT("EquippedItemComponent"));
equippedItemComponent->SetupAttachment(RootComponent);
The hierarchy is like this:
RootComponent->staticMesh
So staticMesh is not the root.
staticMesh is the root of the APickupItem?
isn’t that already a problem?
if I remember correctly when we tried to simulate a non root component it kind of feels like it’s detached from the root component (moves independently of the actual actor),
what I would expect to happen is:
- Simulating physics on the non root component leads to it getting moved away from the original distance to its root component
- Stopping simulating physics freezes that new distance to the root component
- Attaching the root component with snap to target moves the root component to the target location
- The static mesh moves to keep the relative distance to the root it had, at the time when you stopped physics simulation.
Ah OK, I didn’t know that’s what really happened. I thought that it dragged the whole actor with it… Thanks for the answer, I managed to fix the problem like you said… Made staticMesh my RootComponent.