# Attached object not following character rotation

**URL:** <https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [January 26, 2017, 3:32pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706 "2017-01-26T15:32:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_26265182](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_26265182](https://forums.unrealengine.com/u/anonymous_user_26265182)\
**Post date:** [January 26, 2017, 3:32pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/1 "2017-01-26T15:32:07Z")

</div>

Finally got it to where my play can pick up a weapon and it is attached however for some reason the rotation isn’t followed especially when looking up and down.

![WeapCap.JPG](https://d3kjluh73b9h9o.cloudfront.net/original/3X/0/2/0278966f808e0e4b07624b3ca37e0de6071373e2.jpeg)

and bellow is the line of code that attached the weapon to the player character.

mWeapon-\>AttachToComponent(APlayerCharacter::GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT(“HandSock”));

what am I doing wrong because I don’t see a option to use parent rotation?

---

<div class="post-metadata">

**Author:** ![hard\_code](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hard\_code](https://forums.unrealengine.com/u/hard_code)\
**Post date:** [January 26, 2017, 4:28pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/2 "2017-01-26T16:28:04Z")

</div>

Try the AttachTo method with EAttachLocation::Type SnapToTarget

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_26265182](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_26265182](https://forums.unrealengine.com/u/anonymous_user_26265182)\
**Post date:** [January 26, 2017, 4:45pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/3 "2017-01-26T16:45:04Z")

</div>

When I try using AttachTo it says will become deprecated or something like that and to use AttachToComponent, but I will try again.

---

<div class="post-metadata">

**Author:** ![hard\_code](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hard\_code](https://forums.unrealengine.com/u/hard_code)\
**Post date:** [January 26, 2017, 5:19pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/4 "2017-01-26T17:19:12Z")

</div>

Ah i just checked my code and see i switched everything to K2\_AttachToComponent probably for that reason.

K2\_AttachToComponent(GetCharacter()-\>GetMesh(), RightWeaponEquipSlot, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true);

This is the source of K2\_AttachToComponent

return AttachToComponent(Parent, FAttachmentTransformRules(LocationRule, RotationRule, ScaleRule, bWeldSimulatedBodies), SocketName);

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_26265182](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_26265182](https://forums.unrealengine.com/u/anonymous_user_26265182)\
**Post date:** [January 27, 2017, 3:39pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/5 "2017-01-27T15:39:27Z")

</div>

mWeapon-\>AttachToComponent(APlayerCharacter::GetMesh(), FAttachmentTransformRules::FAttachmentTransformRules(EAttachmentRule::KeepRelative, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, false), TEXT(“HandSock”)); now when I pick up the rifle it some how goes beneath the floor but follows the player even with all of them set to snaptotarget.

---

<div class="post-metadata">

**Author:** ![hard\_code](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hard\_code](https://forums.unrealengine.com/u/hard_code)\
**Post date:** [January 27, 2017, 7:54pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/6 "2017-01-27T19:54:58Z")

</div>

Did you set the weapon as a preview in the skeletalmesh socket and make sure it lines up when attached to the socket? Also make sure your collisions are not causing issues with the weapon and the pawn. Other than that I am not sure.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_26265182](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_26265182](https://forums.unrealengine.com/u/anonymous_user_26265182)\
**Post date:** [January 28, 2017, 3:43pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/7 "2017-01-28T15:43:37Z")

</div>

Ill look into collision settings but other than that I dont know. Doe it need to be spawned to work properly? Because my weapon is allready on the ground and when the player presses A it gets picked up.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_26265182](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_26265182](https://forums.unrealengine.com/u/anonymous_user_26265182)\
**Post date:** [January 28, 2017, 4:58pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/8 "2017-01-28T16:58:33Z")

</div>

I don’t think it has anything to do with collision for some reason my socket is not being found.

```auto

WSOCK = Cast<USkeletalMeshSocket>(GetMesh()->GetSocketByName( TEXT("HandSock")));
//mWeapon->AttachRootComponentTo(GetMesh(), "HandSock", EAttachLocation::SnapToTarget, false);
mWeapon->K2_AttachToComponent(APlayerCharacter::RootComponent, TEXT("HandSock"),EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget EAttachmentRule::SnapToTarget, false);

```

I added the WSOCK line just to see if I could see if it is even finding the socket which I don’t think it is because it just shows USkeletalMeshSocket instead of HandSock. I have verified that the spelling is correct. Hopefully that helps a little, because I’m seriously stumped.

---

<div class="post-metadata">

**Author:** ![hard\_code](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hard\_code](https://forums.unrealengine.com/u/hard_code)\
**Post date:** [January 28, 2017, 8:13pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/9 "2017-01-28T20:13:19Z")

</div>

If the socket is not found then it will attach at the ground so ya thats probably your issue. You can call DoesSocketExist to check [USkinnedMeshComponent::DoesSocketExist | Unreal Engine Documentation](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/USkinnedMeshComponent/DoesSocketExist/index.html)

Try changing TEXT(“HandSock”) to FName(“HandSock”) is my guess. Other than that recreate the socket in the skeletalmesh editor maybe.

Edit: Oh and not sure if APlayerCharacter::RootComponent works. I just call GetMesh() for that so it should be

mWeapon-\>K2\_AttachToComponent(GetMesh(), FName(“HandSock”) , EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true);

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_26265182](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_26265182](https://forums.unrealengine.com/u/anonymous_user_26265182)\
**Post date:** [February 2, 2017, 4:13pm UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/10 "2017-02-02T16:13:59Z")

</div>

Still isn’t being found this is really becoming frustrating. At first I though I found the issue, when I created the socket I guess I only created a default socket and not a meshsocket but that didn’t fix anything. I hope using a source build isn’t the issue. I guess I will go back through trying the various attach to commands and if that doesn’t work I guess I will try using the default 3rd person skeletal mesh to see if its an issue with my rig or code.

UPDATE.  
Even after using a BP to attach it either it appears behind above or keeping world it moves with the character in front of the player character. Has anyone tried doing this using a source build?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_26265182](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_26265182](https://forums.unrealengine.com/u/anonymous_user_26265182)\
**Post date:** [February 6, 2017, 1:28am UTC](https://forums.unrealengine.com/t/attached-object-not-following-character-rotation/83706/11 "2017-02-06T01:28:08Z")

</div>

Is there any performance to gain by attaching an object through C++ or is it sufficient to do it with Blueprints? I know that in the end C++ will beat anything, but just curious as to the actual performance difference on this.
