Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Reading from CSV

Discussion in 'Scripting' started by unity_4J-FEv3jCWkBSQ, Feb 29, 2020.

  1. unity_4J-FEv3jCWkBSQ

    unity_4J-FEv3jCWkBSQ

    Joined:
    Jan 6, 2020
    Posts:
    9
    I am trying to move an object based on acceleration and angle values by reading the same from a CSV file. The script i wrote shows no error but the object is not moving. What should i add to move the object?
    Code (CSharp):
    1. private Rigidbody rigid;
    2.     float smooth = 5.0f;
    3.  
    4.     void Start()
    5.     {
    6.         rigid = GetComponent<Rigidbody>();
    7.         StartCoroutine(MyCoroutine());
    8.     }
    9.  
    10.     IEnumerator MyCoroutine()
    11.     {
    12.         TextAsset Sensor = Resources.Load<TextAsset>("Sensor");
    13. string[] data = Sensor.text.Split(new char[] { '\n' });
    14.         for (int i = 1; i < data.Length - 1; i++)
    15.         {
    16.             string[] row = data[i].Split(new char[] { ',' });
    17.             Data q = new Data();
    18.            
    19.             float.TryParse(row[0], out q.accx);
    20.             float.TryParse(row[1], out q.accy);
    21.             float.TryParse(row[2], out q.accz);
    22.             float.TryParse(row[3], out q.gyrox);
    23.             float.TryParse(row[4], out q.gyroy);
    24.             float.TryParse(row[5], out q.gyroz);
    25.            
    26.             Quaternion target = Quaternion.Euler(q.gyrox, q.gyroy, q.gyroz);
    27.             transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
    28.  
    29.             if (q.accx == 0 && q.accy == 0 && q.accz == 0)
    30.             {
    31.                 rigid.velocity = Vector3.zero;
    32.             }
    33.             else
    34.             {
    35.             rigid.AddForce(q.accx * Time.deltaTime, q.accy * Time.deltaTime, q.accz * Time.deltaTime, ForceMode.Acceleration);
    36.             }
    37.  
    38.             yield return new WaitForSeconds(1);
    39.         }
    40.     }
     
  2. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    509
    I don't know the solution, but your AddForce call with ForceMode.Acceleration seems weird.
    You only call AddForce once and then wait for a second before you process the next line.

    Usually ForceMode.Acceleration is used if you call AddForce in multiple (physics) frames and not for "call once" AddForce calls like in your example.

    And you multiply all your values by Time.DeltaTime while the AddForce call itself will also multiply the values by Time.fixedDeltaTime because of the ForceMode.Acceleration, I assume that this makes your values so low, that you don't notice any movements.

    Maybe you could try a different force mode or set the velocity directly, im not sure which is the best solution for your case, but I think that the AddForce line is the problem.

    Tbh Im not sure if using Time.DeltaTime in combination with yield return new WaitForSeconds(1) is a good idea, because you will only get the DeltaTime of the "current" frame, but your actual delta is 1 second.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    it's not. just to confirm this.
     
    unity_4J-FEv3jCWkBSQ likes this.
  4. unity_4J-FEv3jCWkBSQ

    unity_4J-FEv3jCWkBSQ

    Joined:
    Jan 6, 2020
    Posts:
    9
    We removed it but still the object is not moving
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    check if your rigidbody has is kinematic turned on and turn it off.
    a screenshot of your inspector would help as well