- Home /
how to set the value of my start time?
Hi, I am making a simulation in which the code reads the data from an excel sheet and makes a list. For example, in the list, it might say that at 100th sec, an object moves and at 105th sec it moves to another place. I would like to know how to set the start time of the simulation as 100th sec. So, the first second in the simulation = 100th sec and the 5th sec in simulation = 105th sec.
Below is the code I have written for reading the excel sheet and importing that data to a list.
/Sample Excel sheet data: Time ; Object no. ; Object coordinates 100th ; 1 ; (1,2,3) 105th ; 1 ; (2,2,3) 110th; 1 ; (4,2,3 ) 110th ; 2 ; (14,2,3) 115th ; 1 ; (6,2,3 ) 115th ; 2 ; (18,2,3) /
public class CSVData
{
public int simsec;
public int vnum;
public string vtype;
public float cfx;
public float cfy;
public float cfz;
}
List<CSVData> csv = new List<CSVData>();
public void Start()
{
TextAsset traffic = Resources.Load<TextAsset>("traff");
string[] data = traffic.text.Split(new char[] { '\n' });
Debug.Log(data.Length);
for (int i = 25; i < data.Length - 1; i++)
{
string[] row = data[i].Split(new char[] { ',' });
CSVData c = new CSVData();
int.TryParse(row[0], out c.simsec);
//c.vtype = row[2];
int.TryParse(row[1], out c.vnum);
c.vtype = row[2];
float.TryParse(row[3], out c.cfx);
float.TryParse(row[4], out c.cfy);
float.TryParse(row[5], out c.cfz);
csv.Add(c);
}
Could you give us an example of what you'd like to happen? What is getting increased, and where? Be specific.
Answer by andrewsmithparkside · Dec 19, 2019 at 04:44 PM
I'm not quite sure what the second list is for aside from the tryparse, but do you want a whole new list every time you hit that suggested time, or just update the old list?
Anyways, I'd do something like,
float timestart = 0; timestart += Time.DeltaTime;
if timestart > timevarhere && timestart < timevar+1.2 (just to make sure it's not constantly doing it) { stuff here }
or if you want it to be neater int varint = 0;
if timestart > timevarhere && varint = 0 { stuff here var += 1; } Which will ensure it will only do it once.
I'm not sure what second list u are talking about. An can you explain what is suppose to be?
Answer by Statement · Dec 19, 2019 at 06:23 PM
Perhaps something like this, where you add a startTime variable to offset the elapsed time and fast forward the simulation in Start according to your (hidden to us) logic.
using System.Collections.Generic;
using UnityEngine;
public class CSVRead : MonoBehaviour
{
// Change this to 100 if you want to start at 100th second.
public float startTime = 0.0f;
public class CSVData
{
public int simsec;
public int vnum;
public string vtype;
public float cfx;
public float cfy;
public float cfz;
}
List<CSVData> csv = new List<CSVData>();
public void Start()
{
SetupCSV();
FastForwardCSV(startTime);
}
void SetupCSV()
{
TextAsset traffic = Resources.Load<TextAsset>("traff");
string[] data = traffic.text.Split(new char[] { '\n' });
Debug.Log(data.Length);
for (int i = 25; i < data.Length - 1; i++)
{
string[] row = data[i].Split(new char[] { ',' });
CSVData c = new CSVData();
int.TryParse(row[0], out c.simsec);
int.TryParse(row[1], out c.vnum);
c.vtype = row[2];
float.TryParse(row[3], out c.cfx);
float.TryParse(row[4], out c.cfy);
float.TryParse(row[5], out c.cfz);
csv.Add(c);
}
}
void FastForwardCSV(float startTime)
{
foreach (var c in csv)
{
if (c.simsec < startTime)
{
// Add code to move object to wherever it needs to be
}
}
}
void Update()
{
float time = Time.time + startTime;
// Your ususal code, using time
}
}
Im not sure how this will work if (c.simsec < startTime)
I have updated my question with more info Could you please check it out
I am unsure what you're asking about. I got the impression you wanted to start placing (driving, whatever) objects (vnum) to locations (cfx etc) at some time (simsec) and that you wanted to skip ahead 100 seconds in your list of objects.
The if (c.simsec < startTime)
is there in FastForwardCSV
to let all objects in your list get a chance to catch up with the simulation being skipped startTime
seconds in time.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220408im_/https://answers.unity.com/themes/thub/images/avi.jpg)