Component has local offset when spawn (located at world 0,0,0)

Had a similar problem…
I would spawn a weapon Actor, and StaticMeshComponent’s relative location would go crazy. I couldn’t get it to reset to 0, and have a gun at socket location. I listened this advice, and it worked like it should. :slight_smile:

Here’s the code:
ExampleWeapon.h


//It is important to use ObjectInitializer version, since RootComponent attachment can get buggy when trying to set it up in the constructor.
    AUrathaWeapon(const FObjectInitializer& OI);

    UPROPERTY(EditDefaultsOnly, Category = "Weapon")
    UBoxComponent* WeaponRoot;

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon")
    UStaticMeshComponent* WeaponMeshComponent;

ExampleWeapon.cpp


AUrathaWeapon::AUrathaWeapon(const FObjectInitializer& OI) : Super(OI)
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    //This is a recommendable way to create sub object when attachment to RootComponent is buggy (e.g. relative location is screwed).
    WeaponRoot = OI.CreateDefaultSubobject<UBoxComponent>(this, FName("WeaponRoot"));
    SetRootComponent(WeaponRoot);

    WeaponMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(FName("WeaponMeshComponent"));
    //Use this instead of AttachToComponent when doing this in the constructor. Interestingly enough, AttachToComponent works as intended in Blueprint.
    WeaponMeshComponent->SetupAttachment(WeaponRoot);
}

ExampleCharacter.cpp


if (WeaponBlueprint)
    {
        //UE4 sometimes struggles with passing socket name as TEXT("SocketName"). Using this instead...
        FName SocketName("WeaponSocket");
        FActorSpawnParameters spawnParams;
        spawnParams.Owner = this;

        //Spawn weapon at 0,0,0 with 0,0,0 rotation. A precaution...
        Weapon = GetWorld()->SpawnActor<AUrathaWeapon>(WeaponBlueprint, FVector(0.f, 0.f, 0.f), FRotator(0.f, 0.f, 0.f), spawnParams);
        UE_LOG(LogTemp, Warning, TEXT("Weapon spawned!"));

        //Attach to player's hand. FINALLY WORKING!
        Weapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, SocketName);
    }