# How to force the hair mesh to follow animation?

**URL:** <https://forums.unrealengine.com/t/how-to-force-the-hair-mesh-to-follow-animation/483704>\
**Category:** C++\
**Tags:** unreal-engine, UE4-26, CPP, question\
**Created:** [May 21, 2021, 8:36am UTC](https://forums.unrealengine.com/t/how-to-force-the-hair-mesh-to-follow-animation/483704 "2021-05-21T08:36:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [May 21, 2021, 8:36am UTC](https://forums.unrealengine.com/t/how-to-force-the-hair-mesh-to-follow-animation/483704/1 "2021-05-21T08:36:00Z")

</div>

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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/5/7/c574b8b762cbe6e03666871f25247db5c3df16bb.gif)

---

<div class="post-metadata">

**Author:** ![Fraps2020](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fraps2020/32/460865_2.png) [@Fraps2020](https://forums.unrealengine.com/u/Fraps2020)\
**Post date:** [May 21, 2021, 12:56pm UTC](https://forums.unrealengine.com/t/how-to-force-the-hair-mesh-to-follow-animation/483704/2 "2021-05-21T12:56:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [May 21, 2021, 1:35pm UTC](https://forums.unrealengine.com/t/how-to-force-the-hair-mesh-to-follow-animation/483704/3 "2021-05-21T13:35:52Z")

</div>

Not working friend

---

<div class="post-metadata">

**Author:** ![Fraps2020](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fraps2020/32/460865_2.png) [@Fraps2020](https://forums.unrealengine.com/u/Fraps2020)\
**Post date:** [May 21, 2021, 2:42pm UTC](https://forums.unrealengine.com/t/how-to-force-the-hair-mesh-to-follow-animation/483704/4 "2021-05-21T14:42:39Z")

</div>

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](https://docs.unrealengine.com/en-US/AnimatingObjects/SkeletalMeshAnimation/WorkingwithModularCharacters/index.html))

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [May 21, 2021, 3:38pm UTC](https://forums.unrealengine.com/t/how-to-force-the-hair-mesh-to-follow-animation/483704/5 "2021-05-21T15:38:48Z")

</div>

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.

 ![339113-img-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/7/e/e7ec67b50b2cba24664ee09c43d2e496122dc3b6.png)
