How to get a bone location in world space in C++?

I’m doing this in a ActorComponent of a Character.

here’s my code:

AActor* characterActor = NULL;

characterActor = GetOwner();

playermesh = Cast (characterActor->GetComponentByClass(USkeletalMeshComponent::StaticClass()));

FVector head_loc = playermesh->GetBoneLocation(“head”, EBoneSpaces::WorldSpace);

But I found that the result location head_loc is all zero.(0, 0, 0)
what’s wrong with my code ?

1 Like

Check out bone name. In some cases bone names in Unreal are not the same with those in model (prefixes, suffixes can be added). Perhaps that helps.

Hi,

Give this a shot.

You will need to #include your character’s header in the cpp.
This should work… hope it helps.

#include "<put your character's header in cpp>"

// Called when the game starts
void UMyActorComponent::BeginPlay()
{
	Super::BeginPlay();

        // Get the owner of this component
	AActor* CharacterOwner = GetOwner();

        // Cast the owner to my character
	AMyCharacter* MyCharacter = Cast<AMyCharacter>(CharacterOwner);

        // check the cast is valid
	if (MyCharacter )
	{
                // Get the bone of interest.
                // Double check in PERSONA that name and spelling match!!!
		FName HeadBone = "Head";
		FVector BoneLocation = MyCharacter->GetMesh()->GetBoneLocation(HeadBone,EBoneSpaces::WorldSpace);
		UE_LOG(LogTemp, Warning, TEXT("Head bone location is : %s"), *BoneLocation.ToString());
	}

}