I have an Equipment class to represent items that can be picked up and put into the players hand. I want to use Actor->AttachToComponent() to do this.
void APlayerCharacter::EquipItem(AEquipment* Equipment)
{
if (Equipment == nullptr) return;
UE_LOG(LogTemp, Warning, TEXT("Equipping %s"), *Equipment->GetActorNameOrLabel());
FName Socket = TEXT("RightHandGripPoint");
FAttachmentTransformRules Rules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, false);
Equipment->SetSimulatePhysicsAndCollision(false);
Equipment->AttachToComponent(GetMesh(), Rules, Socket);
}
I’m really not sure what part of the code is causing this issue. I know that there are usually a set of common issues that I’ve gone over and double checked aren’t the issue, these would be…
- Ensuring the socket name is correct (used copy paste and validated by eye).
- Equipment is definitely not a nullptr, the UE_LOG confirms that the interacted item is the one I want.
- I’ve seen that it’s important to turn off the items physics and collision before attaching to the interacting character, so this is done here…
void AEquipment::SetSimulatePhysicsAndCollision(bool bSimulatePhysics)
{
if (Mesh)
{
Mesh->SetSimulatePhysics(bSimulatePhysics);
if (bSimulatePhysics)
{
Mesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
}
else
{
Mesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
}
}
}
Any help getting the equipment to attach properly is appreciated. I’m 99% sure the problem lies in the EquipItem() function, since the interface that calls this function handles every other case exactly as intended.