I am trying to make a third person character pick up and carry an actor (a log, which is a blueprint class containing a static mesh component). The character is using the UE4 Mannequin skeleton and I’m trying to attach the actor to the right hand socket.
In c++ I have gotten to the stage of the log actor snapping to the character using AttachToComponent, attaching the log actor to the Mesh component of the character at the right hand socket. As the character walks around the log follows, but the log doesn’t follow the right hand socket’s relative translation and instead seems attached to the character root.
The weird thing is that after the attachment, calling GetAttachParentSocketName on the log at the end does return the right hand socket name so it seems to be attaching to the correct socket.
This has turned out to be fairly difficult to google an answer for, and the answer seems to be that I should add a static mesh component to the character blueprint and when the character picks up the log, I set the static mesh of the component on the character to the mesh of the log, rather than actually attaching the log actor to the character. Is this actually the only way to do it? It kind of sucks if my actor is more than just a simple static mesh, such as a few static meshes combined dynamically.
I’m not sure if my code will be particularly useful since I’ve tried a dozen variations - combinations of attachment rules, with and without collision, with and without the static mesh component being the root component of my ‘log’ blueprint etc. Here’s the latest (I plan to refactor this, I’m just prototyping ideas and learning more about UE4):
AActor* post = GetWorld()->SpawnActor<AActor>(structureToBuild->BuildingPostSolidMesh);
FName socket = TEXT("hand_rSocket");
USkeletalMeshComponent* mesh = GetCharacter()->GetMesh();
const USkeletalMeshSocket* socketInstance = mesh->GetSocketByName(socket);
FAttachmentTransformRules rules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true);
post->AttachToComponent(mesh, rules, socketInstance->GetFName());
auto test = post->GetAttachParentSocketName();
Thanks in advance.