Is there a simple way to get VR controllers' XYZ world location in C++?

This seems straightforward to me (I was hoping for a GetLeftControllerXYZ() sorta function), but all the solutions I’ve found are complex, difficult to implement, and involve blueprints.

I have to say it’s not something I’ve tried myself but you should be able to get the LeftController (for example) and get the transform from it. I believe this is relative to the owner, in the case of the course the VRCharacter. There is a method that can take local coordinates and transform relative to the owner, or, at a push, just add the left controller’s position to the owner’s world position. I can’t say if this will work but give it a try.

As it happens, using BluePrint isn’t an issue. You could expose a blueprint function to C++ and call

I gave up on trying to do it entirely in C++, but I can’t get it to work with blueprints either. I feel like I’m close but I’m missing something.

Here’s the entire (very simple) project, can somebody help me figure out what I’m missing?

There must be a way of doing this. I mean, what if you want to attach your own mesh at the controller position?

I did do a Unity project years ago now and we attacked an object positioned on the right hand showing just past the controller. This worked. UE is way better so there has to be a way.

I suspect the controller must be attached as a sub-component during run-time and there has to be a way to get it. The rendered mesh for the controllers should have a position but it’s finding that. Try looking in details for the BP that is instanced and see what is going on.

I’ll take a look when I can…but it may be some time next week.

Thanks; yes, please let me know.

I sat down with a programmer friend for 2 hours with this the other night and the two of us were unable to get it either.

We tried a handful of different approaches, including finding the motion controller mesh that Unreal is drawing, but we couldn’t figure out where that was coming from or how to access it.

I didn’t think this would be this difficult or time consuming, but I’m thinking I don’t understand something about the way Unreal draws the motion controllers in VR, because I can’t seem to use that either.

TLDR; I’m still looking for help with this!

I’m making a little progress.

I found the object:

Begin Object Class=/Script/HeadMountedDisplay.MotionControllerComponent Name=“MotionControllerRight”
MotionSource=“Right”
CurrentTrackingStatus=Tracked
bDisplayDeviceModel=True
RelativeLocation=(X=-34.263870,Y=-223.315048,Z=49.243843)
RelativeRotation=(Pitch=-48.272419,Yaw=-108.037369,Roll=84.909813)
UCSSerializationIndex=0
bNetAddressable=True
CreationMethod=SimpleConstructionScript
End Object

so I want the code to be something like

FVector ControllerLocation = GetWorld()-> HeadMountedDisplay.MotionControllerComponent.MotionControllerRight;

but I know that’s not right.

I read thru the whole header file as well as
https://docs.unrealengine.com/4.26/en-US/API/Runtime/HeadMountedDisplay/UMotionControllerComponent/
and a dozen other related pages and examples, I’m still struggling

1 Like

Thanks for sharing. It’ll help me when I take a look.

Any luck?

Nothing yet, sorry :frowning:

Hi. I managed to get some more time on this. I based this on the C++ VRCharacter created in the course and am using an Oculus Quest 2 to test.

I tried a bunch of things but in the end, I went back to the beginning and ignored the previous findings.

So, I added the transform from the hand controller but that didnt work but then I tried the following in the tick

’ UE_LOG(LogTemp, Warning, TEXT(“Left Hand is at: %s”), *(TheLeftMotionController->GetComponentLocation().ToString()))`

This actually works. It outputs the absolute position of the hand of the character.

I then added, to confirm,
’ DrawDebugPoint( GetWorld(), TheLeftMotionController->GetComponentLocation(), 20, FColor::Red, true );’

See the screenshot. It’s a bit messy and tricky to grab but I managed to get it. The red dots appear roughly centre of the controller.

I hope this helps.

Edit: I also changed the Hand Tracking Support in Project Settings to Controllers and Hands but not sure if that actually makes a difference. It probably should be set to controllers at least.

1 Like

Thank you; I’ve been so sick my brain’s not working right, I’ll try this asap tho