Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Game object location extraction as a CSV

Discussion in 'VR' started by zdsmith, Apr 14, 2022.

  1. zdsmith

    zdsmith

    Joined:
    Apr 12, 2022
    Posts:
    5
    Hello all,

    I am putting together an experiment in which I need to collect the position data of a ball that is controlled by the subject each time they pull the left trigger. The idea is that when the subject pulls the trigger the location (x,y,z coordinates) of the game object (Sphere) is appended to a CSV file. I should also mention this is using an Occulus Quest 2 headset and controllers. Below is the Code I have put together. Any help anyone is willing to give is greatly appreciated.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;
    using System.IO;
    using System.Text;

    public class Data : MonoBehaviour
    {
    PlayerControls controls;

    void Awake()
    {
    controls = new PlayerControls();

    controls.DataCollection.CollectPosition.performed += ctx => DataGet();
    }

    void DataGet()
    {
    Vector3 stuff = GameObject.Find("Sphere").transform.position;
    string filePath = "C:/Users/Admin/Downloads/positions.txt";

    float[] output = new float[]
    {
    stuff.x,
    stuff.z,
    stuff.y,
    };

    int length = output.Length;

    StringBuilder sb = new StringBuilder();
    for (int index = 0; index < length; index++)
    sb.AppendLine(string.Join(",", output[index]));

    if (!File.Exists(filePath))
    File.WriteAllText(filePath, sb.ToString());
    else
    File.AppendAllText(filePath, sb.ToString());
    }

    void OnEnable()
    {
    controls.DataCollection.Enable();
    }

    void OnDiasable()
    {
    controls.DataCollection.Disable();
    }
    }