Get MotionController Location in C++

I’ve got my motion controllers setup like this:

Pawn.h

// Left motion controller
UPROPERTY(EditDefaultsOnly)
class UMotionControllerComponent* LeftMotionController;

// Right motion controller
UPROPERTY(EditDefaultsOnly)
class UMotionControllerComponent* RightMotionController;

Pawn.cpp

USceneComponent* RootSceneComponent = CreateDefaultSubobject<USceneComponent>  (TEXT("RootComponent"));
RootComponent = RootSceneComponent;
LeftMotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("LeftMotionController"));
LeftMotionController->SetupAttachment(RootComponent);
RightMotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("RightMotionController"));
RightMotionController->SetupAttachment(RootComponent);

How do I go about getting each controllers world location? I thought it would be something similar to LeftMotionController->GetWorldLocation() but that function isn’t available. In blueprints I would use GetHandPositionAndLocation but I can’t figure out how to access that in c++ either.

First I had to set the default left and right hands for the motion controllers.

//Left
LeftMotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("LeftMotionController"));
LeftMotionController->Hand = EControllerHand::Left;
LeftMotionController->SetupAttachment(RootComponent);		
	
//Right
RightMotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("RightMotionController"));
RightMotionController->Hand = EControllerHand::Right;
RightMotionController->SetupAttachment(RootComponent);

Then I can use GetComponentLocation to get the position in the world.

FVector LeftPos = LeftMotionController->GetComponentLocation();
FVector RightPos = RightMotionController->GetComponentLocation();

You can check the positions in your log by using:

UE_LOG(LogTemp, Log, TEXT("Left Position is %s"), *LeftPos.ToString());
UE_LOG(LogTemp, Log, TEXT("Right Position is %s"), *RightPos.ToString());

Hi, did this work?

It’s so funny… This only marginally works for me. I am checking the actor during Tick logging the location of my occulus controllers, but they always have the same values, no matter what I do. I have tried well over 100 variants.

Have you tried activating the components? The controllers Component Tick() method is what updates the location and orientation.
LeftMotionController->Activate(true);
RightMotionController->Activate(true);