How to force the hair mesh to follow animation?

How to set the hair mesh to follow the animation? #UE-59094 BUG still not solved.

.cpp
Hair = CreateDefaultSubobject<USkeletalMeshComponent>("Hair");
    Hair->SetupAttachment(GetMesh());
    Hair->AttachToComponent(Hair, FAttachmentTransformRules::SnapToTargetNotIncludingScale);

.h
UPROPERTY(EditAnywhere, Category = "Components")
    class USkeletalMeshComponent* Hair;

I have attached the hair mesh to the Head socket but the hair mesh is not following the animation.
hair mesh is called Hair_Mesh and has been attached in the editor while the Hair skeletal mesh component is already sets to UPROPERTY(EditAnywhere, Category = “Components”)

339408-problem-example.gif

Hi c0r37py,

it doesn’t seem you’re actually attaching your Hair to your head socket; the socket is specified as second argument in SetupAttachment (and if you shouldn’t need the “attachtocomponent” afterwards). Just try

Hair->SetupAttachment(GetMesh(), TEXT("headSocket"));

headSocket should be the name of your socket (and again, you do not need that attachToComponent afterwards – you’re already attaching your hair with setupattachment). Also, either you attach this in code or in the editor; looks like you’re prone to the former approach, and you’re doing it right with your UPROPERTY.

Hope that helps!

Cheers
f

Not working friend

Ok. I’d try this next then.

In your .h file, define a USceneComponent reference:

UPROPERTY(EditAnywhere)
	USceneComponent* USC_HeadSocket;

Attach that to your mesh socket in the costructor (assuming your socket as defined in your skeleton is called “headSocket”):

USC_HeadSocket = CreateDefaultSubobject<USceneComponent>(TEXT("myHeadSocket"));
USC_HeadSocket->SetupAttachment(GetMesh(), TEXT("headSocket"));

Then in your PostInitializeComponents (or any other place that’s not your constructor) attach the hair mesh to the scene component:

Hair->AttachToComponent(USC_HeadSocket, FAttachmentTransformRules::SnapToTargetIncludingScale);

If even this does not work, then you can probably switch approach defining your character thorugh a MasterPoseComponent instead (Working with Modular Characters | Unreal Engine Documentation)

Your Solution works but only when the game is in play mode.
In the editor the hairs and the head socket is still not welded.
I have tried with the blueprints and works perfectly but I want the same thing to achieve in C++ code.