Mesh attached to socket displaces during runtime


I have a C++ Character that I have turned into a BP class. The character is the FPS template character with some modifications. I have some issues with attaching weapons to the sockets of my skeletal mesh and hope someone might have an idea what could be wrong.

In the constructur of th character class I setup the components like this:

[SPOILER]// Create a mesh component that will be used when being viewed from a ‘1st person’ view (when controlling this pawn)
Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT(“CharacterMesh1P”));
Mesh1P->bCastDynamicShadow = false;
Mesh1P->CastShadow = false;

// Create a CameraComponent
FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT(“FirstPersonCamera”));
FirstPersonCameraComponent->SetupAttachment(Mesh1P, TEXT(“FPPCamera_socket”));
FirstPersonCameraComponent->bUsePawnControlRotation = true;

// Create a left hand mesh component
FP_LeftHand = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT(“FP_LeftHand”));
FP_LeftHand->SetOnlyOwnerSee(true); // only the owning player will see this mesh
FP_LeftHand->bCastDynamicShadow = false;
FP_LeftHand->CastShadow = false;
FP_LeftHand->SetupAttachment(Mesh1P, TEXT(“Shield_socket”));

// Create a right hand mesh component
FP_RightHand = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT(“FP_RightHand”));
FP_RightHand->SetOnlyOwnerSee(true); // only the owning player will see this mesh
FP_RightHand->bCastDynamicShadow = false;
FP_RightHand->CastShadow = false;
FP_RightHand->SetupAttachment(Mesh1P, TEXT(“1hSword_socket”));[/SPOILER]

When I open my derived BP in the editorthings look fine (well at least the weapons are attached where i expect them to)

But when i hit the play/simulate button the items pops out of the sockets and end up at the characters feet. They are also pretty static and not moving according to the animation.

If anyone have any ideas why this happens I would appreciat it :slight_smile:

Few things I have checked:

  1. Socket names are correct. Else they would not place the weapons in the preview.
  2. Meshes are set to movable.
  3. I have tried to disable gravity for the meshes
  4. I have tried to swap the meshes with the template FP_Gun mesh, this renders same result (however this mesh gets animated)

Thanks in advance;base64

Greetings again everyone.

A bit embarassing but I have already found the solution. Reading a comment in the “BeginPlay” function made me realize that the skeletal mesh is not yet created in the constructor. Thus the meshes cannot be attached to it …

I added this code to the BeginPlay(); function:
//Attach weapon mesh component to Skeleton, doing it here because the skeleton is not yet created in the constructor
FP_LeftHand->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT(“Shield_socket”));
FP_RightHand->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT(“1hSword_socket”));

Things now work as expected :slight_smile: