Search Unity

Using Headset and Controller positions as variables

Discussion in 'AR/VR (XR) Discussion' started by Senseweeks, Jan 13, 2022.

  1. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11
    I am doing a project that requires me to find the centroid of the triangle created from the headset and controllers every frame then draw a line between the centroids over time. To do this I need to get the position in the Y and X directions of the headset and both controllers. I have tried my best and looked around the internet but haven't managed it in the slightest. This is my first time using unity and I'm on a time crunch because the deadline is approaching. Please help this noob out! I'm using the quest 2 and the included controllers I have both the oculus and XR integrations installed (the latest version for both) and I have Unity 2020.3.25f1.

    EDIT: Specifically I need help either using the position values in an equation and printing the result to a file.
     
    Last edited: Jan 13, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    FYI: There's a dedicated set of sub-forums for AR/VR/XR here I'll move your post.
     
  3. kyle_v

    kyle_v

    Unity Technologies

    Joined:
    May 24, 2016
    Posts:
    21
    Hi I can try to help,
    If you already have a basic VR scene working with headset and controllers moving around, you can check their position values by adding a script like this to your project, adding it onto any GameObject and then in the Inspector assign references to the head, handR, and handL GameObject fields.


    Code (CSharp):
    1.  
    2. public class MyScriptName : MonoBehaviour
    3. {
    4.  
    5. public GameObject head;
    6. public GameObject handR;
    7. public GameObject handL;
    8.  
    9. void Update()
    10. {
    11. var headPosition = head.transform.position;
    12. var handRPosition = handR.transform.position;
    13. var handLPosition = handL.transform.position;
    14.  
    15. }
    16.  
    17. }
    18.  
    the position will be of type Vector3 in world space, and you can then do the centroid calculation using headPosition.x , headPosition.y etc..

    If the VR headset and controllers are not moving in the scene when you move the physical devices, then you probably need to compare with an example scene
     
  4. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11

    IT WORKED!! Thank you so much! with this ill be able to actually calculate the centroid position (which I have done) I am now having trouble printing the result to anything other than the console. Also since I want to create a line between the centroids I was thinking to spawn a sort of cube there in-game space to record the line later but have no idea how to start going about that.
     
  5. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11
    I am now trying to create a (preferably permanent) cube at the centroid location. I have tried using instantiate and it does create the cube however it gives my player a strong velocity in the "opposite" direction. Any ideas?

    EDIT: Partially Solved! I removed the cubes collider however, I can't find how to make them permanent
     
    Last edited: Jan 15, 2022
  6. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    I think you have to learn about the basics of Unity before diving into the forums at every wall you hit, however you are doing things right by learning the engine this way.