Issue with exporting CSV file

i am using unity v2018 on a msi laptop i5, 8th gen, windows 10, graphics card NVIDIA geforce 1050. I am impoting a csv file with time and x axis and y axis positions. I am trying to move the object to the specified position based on time from the CSV file to create a real time-interaction. I have used the following code to do the same:
code:

void Start ()
{
TextAsset time = Resources.Load(“time”);
string[ ] data = time.text.Split (new char[ ] { ‘\n’ });
for (int i = 1; i < data.Length - 1; i++)
{
string[ ] row = data*.Split(new char[ ] { ‘,’ });*
Data q = new Data();
int.TryParse(row[0],out q.time);
int.TryParse(row[1],out q.x);
int.TryParse(row[2],out q.z);
pos.Add(q);
if (q.time == 0)
transform.position = transform.position + new Vector3(q.x * movementSpeed * Time.deltaTime, 0, q.z * movementSpeed * Time.deltaTime);
if (q.time == 10)
transform.position = transform.position + new Vector3(q.x * movementSpeed * Time.deltaTime, 0, q.z * movementSpeed * Time.deltaTime);
if (q.time == 20)
transform.position = transform.position + new Vector3(q.x * movementSpeed * Time.deltaTime, 0, q.z * movementSpeed * Time.deltaTime);
}
}
This is the class i used for importing:
public class Data
{
public int time;
public int x;
public int z;
}
this is the csv file:
time, x, z
0, 0, 0
10, 10, 20
20, 20, 10
Instead of the object moving twice to different positions the object is moving finally to a position which seems to be the sum of the two positions i provided, i.e. 30,30 here.
what should i change to get the desired output.

Since you’re doing this in Start, once it hits the for loop, it’s going to end up moving to the end of the list basically with the sum of all movements.

Basically, it hits the for loop, then goes through each line and when it hits the 10 value, it will move a certain amount, but then it keeps going and hits the 20 value which moves a certain amount.

Without a yield effect, you’re not going to see any movement until the loop has done all it’s work, so it’s just a addition of all changes.

Also, please use code tags.

2 Likes

You need to split the movement up so it occurs over time instead of all at the same time in the same frame. You’d do that in Update or a coroutine instead of in Start. In a coroutine you’d yield between movements, and in Update you’d otherwise split the movement into multiple frames.

The way you have it written now, in a single frame everything in Start with be completed.

1 Like