Search Unity

Question Starting / Ending the game on pressing the A button

Discussion in 'XR Interaction Toolkit and Input' started by Senseweeks, Jan 17, 2022.

  1. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11
    I have a program that runs in update and It needs to since I need data every frame however, I would like to start / stop the generation when I push the A button on my Quest 2 controller. This will make it easier to collect the data. I've attached the code that I am currently using. I am having a few more issues and I've posted separate help requests:
    https://forum.unity.com/threads/permanently-instantiate.1226139/


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using UnityEngine.XR;
    6. public class TrackAllPositions : MonoBehaviour
    7. {
    8.     public GameObject CenterEyeAnchor;
    9.     public GameObject RightControllerAnchor;
    10.     public GameObject LeftControllerAnchor;
    11.     public Transform TrackerCube;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         // var headPosition = CenterEyeAnchor.transform.position;
    18.         var headX = CenterEyeAnchor.transform.position.x;
    19.         var headY = CenterEyeAnchor.transform.position.y;
    20.         var headZ = CenterEyeAnchor.transform.position.z;
    21.  
    22.         //var handRPosition = RightControllerAnchor.transform.position;
    23.         var handRX = RightControllerAnchor.transform.position.x;
    24.         var handRY = RightControllerAnchor.transform.position.y;
    25.         var handRZ = RightControllerAnchor.transform.position.z;
    26.  
    27.  
    28.  
    29.         //var handLPosition = LeftControllerAnchor.transform.position;
    30.         var handLX = LeftControllerAnchor.transform.position.x;
    31.         var handLY = LeftControllerAnchor.transform.position.y;
    32.         var handLZ = LeftControllerAnchor.transform.position.z;
    33.  
    34.  
    35.         float centroidX1 = (handRX + handLX + headX);
    36.         float centroidX = centroidX1 / 3;
    37.  
    38.         float centroidY1 = (handRY + handLY + headY);
    39.         float centroidY = centroidY1 / 3;
    40.  
    41.      
    42.  
    43.  
    44.     }
    45.  
    46.  
    47.  
    48.     void CreateText()
    49.     {
    50.  
    51.         var headX = CenterEyeAnchor.transform.position.x;
    52.         var headY = CenterEyeAnchor.transform.position.y;
    53.         var headZ = CenterEyeAnchor.transform.position.z;
    54.  
    55.         //var handRPosition = RightControllerAnchor.transform.position;
    56.         var handRX = RightControllerAnchor.transform.position.x;
    57.         var handRY = RightControllerAnchor.transform.position.y;
    58.         var handRZ = RightControllerAnchor.transform.position.z;
    59.  
    60.  
    61.  
    62.         //var handLPosition = LeftControllerAnchor.transform.position;
    63.         var handLX = LeftControllerAnchor.transform.position.x;
    64.         var handLY = LeftControllerAnchor.transform.position.y;
    65.         var handLZ = LeftControllerAnchor.transform.position.z;
    66.  
    67.  
    68.         float centroidX1 = (handRX + handLX + headX);
    69.         float centroidX = centroidX1 / 3;
    70.  
    71.         float centroidY1 = (handRY + handLY + headY);
    72.         float centroidY = centroidY1 / 3;
    73.  
    74.  
    75.         //Path of File
    76.         string path = Application.dataPath + "/Log.txt";
    77.         //Create File if it doesn't exist
    78.         if (!File.Exists(path))
    79.         {
    80.             File.WriteAllText(path, "Centroid Coordinates in XY on");
    81.             File.AppendAllText(path, File.GetCreationTime(path) + "\n\n ");
    82.         }
    83.  
    84.         //Content of file
    85.         string content = centroidX + ", " + centroidY + "\n";
    86.  
    87.         //Add the text
    88.         File.AppendAllText(path, content);
    89.  
    90.      
    91.  
    92.     }
    93.  
    94.     // Update is called once per frame
    95.     void Update()
    96.     {
    97.         // var headPosition = CenterEyeAnchor.transform.position;
    98.         var headX = CenterEyeAnchor.transform.position.x;
    99.         var headY = CenterEyeAnchor.transform.position.y;
    100.         var headZ = CenterEyeAnchor.transform.position.z;
    101.  
    102.         //var handRPosition = RightControllerAnchor.transform.position;
    103.         var handRX = RightControllerAnchor.transform.position.x;
    104.         var handRY = RightControllerAnchor.transform.position.y;
    105.         var handRZ = RightControllerAnchor.transform.position.z;
    106.  
    107.  
    108.  
    109.         //var handLPosition = LeftControllerAnchor.transform.position;
    110.         var handLX = LeftControllerAnchor.transform.position.x;
    111.         var handLY = LeftControllerAnchor.transform.position.y;
    112.         var handLZ = LeftControllerAnchor.transform.position.z;
    113.  
    114.  
    115.         float centroidX1 = (handRX + handLX + headX);
    116.         float centroidX = centroidX1 / 3;
    117.      
    118.         float centroidY1 = (handRY + handLY + headY);
    119.         float centroidY = centroidY1 / 3;
    120.  
    121.         float centroidZ1 = (handRZ + handLZ + headZ);
    122.         float centroidZ = centroidZ1 / 3;
    123.  
    124.  
    125.         //Debug.Log("Centroid Coordinates in XY Plane = (" + centroidX + ", " + centroidY +")")
    126.  
    127.         Instantiate(TrackerCube, new Vector3(centroidX, centroidY, centroidZ), Quaternion.identity);
    128.  
    129.         CreateText();
    130.  
    131.  
    132.      
    133.  
    134.  
    135.  
    136.  
    137.     }
    138.  
    139.  
    140.  
    141.  
    142.  
    143.  
    144.  
    145.  
    146.  
    147. }
    148.  
     
    Last edited: Jan 17, 2022