Download

Attached object not following character rotation

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

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?

Try the AttachTo method with EAttachLocation::Type SnapToTarget

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

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);

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.

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.

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.

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



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.

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 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);

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?

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.