Download

Attaching to a socket

Hi all, I’m attempting to attach my actor to my player character and I have done so but I don’t want my actor to rotate with the character when it turns left and right. Is there any way I can do this? Thank you.

Here’s my code in BeginPlay():
PlayerCharacter = Cast<APlayerCharacter>(GetWorld()->GetFirstPlayerController()->GetPawn());

if (PlayerCharacter)
{
this->AttachToActor(PlayerCharacter, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::KeepWorld, EAttachmentRule::KeepWorld, false), FName(“TopSocket”));
}

I’m still new to UE so I’m hoping someone else will know a better way, but the way I’d go about it is to use the Tick() function to get the socket world location and then set your actor to that location without attaching it. Then your actor won’t rotate with the character.

Something like this… (psuedo code so probably won’t compile…)



void Tick(float DeltaSeconds)
{
    if (PlayerCharacter)
    {
        FVector WorldSocketLocation = PlayerCharacter.GetSocketLocation(FName("TopSocket"));
        this.SetActorLocation(WorldSocketLocation);
    }
}


Thank you James, this did fix my problem whether or not it is the most efficient way I wouldn’t know myself because I’m am still rather new to ue as well. :slight_smile: