Reading from CSV

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?

private Rigidbody rigid;
    float smooth = 5.0f;

    void Start()
    {
        rigid = GetComponent<Rigidbody>();
        StartCoroutine(MyCoroutine());
    }

    IEnumerator MyCoroutine()
    {
        TextAsset Sensor = Resources.Load<TextAsset>("Sensor");
string[] data = Sensor.text.Split(new char[] { '\n' });
        for (int i = 1; i < data.Length - 1; i++)
        {
            string[] row = data[i].Split(new char[] { ',' });
            Data q = new Data();
           
            float.TryParse(row[0], out q.accx);
            float.TryParse(row[1], out q.accy);
            float.TryParse(row[2], out q.accz);
            float.TryParse(row[3], out q.gyrox);
            float.TryParse(row[4], out q.gyroy);
            float.TryParse(row[5], out q.gyroz);
           
            Quaternion target = Quaternion.Euler(q.gyrox, q.gyroy, q.gyroz);
            transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);

            if (q.accx == 0 && q.accy == 0 && q.accz == 0)
            {
                rigid.velocity = Vector3.zero;
            }
            else
            {
            rigid.AddForce(q.accx * Time.deltaTime, q.accy * Time.deltaTime, q.accz * Time.deltaTime, ForceMode.Acceleration);
            }

            yield return new WaitForSeconds(1);
        }
    }

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.

it’s not. just to confirm this.

1 Like

We removed it but still the object is not moving

check if your rigidbody has is kinematic turned on and turn it off.
a screenshot of your inspector would help as well