Read position data from Csv or txt file to move game object?
Hi All, I'm very inexperienced in coding and unity. I'm trying to move gameobject by reading csv or txt file. Basic circle or rectangle is enough for the beginning. I would be happy if you helped me.
I found this code on the web but it moves on one way forever:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
public class CSVFIleReader : MonoBehaviour
public GameObject Cube;
public TextAsset csvFile;
// Update is called once per frame
void Update()
{
readCSV();
}
void readCSV()
{
string[] records = csvFile.text.Split('\n');
for (int i = 1; i < records.Length; i++)
{
string[] array = records[i].Split(',');
var pos = new Vector3(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]));
}
}
Text file:
speed,x,y,z
1,0,0,1
1,0,2,1
1,0,4,2
Answer by Hellium · Jan 30, 2020 at 10:31 AM
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CSVFIleReader : MonoBehaviour
{
private struct Step
{
public float speed;
public Vector3 destination;
}
public Transform Cube;
public TextAsset CsvFile;
private List<Step> steps;
private int currentStep = 0;
void Start()
{
ReadCSV();
Cube.position = steps[0].destination;
}
void Update()
{
if((Cube.position - steps[currentStep].destination).sqrMagnitude < 0.1f)
currentStep = (currentStep + 1) % steps.Count;
Cube.position = Vector3.MoveTowards(Cube.position, steps[currentStep].destination, steps[currentStep].speed * Time.deltaTime);
}
void ReadCSV()
{
steps = new List<Step>();
string[] records = CsvFile.text.Split('\n');
for (int i = 1; i < records.Length; i++)
{
string[] array = records[i].Split(',');
steps.Add(
new Step()
{
speed = float.Parse(array[0]),
destination = new Vector3(float.Parse(array[1]), float.Parse(array[2]), float.Parse(array[3]))
}
);
}
}
}
Hi @Hellium thanks for the latest answer.
What should be added to code in order to read and take the position of the line:
Children (0): Static Translation: (15.0503, 34.1802, -118.183) from the whole text?
another one: what if there is a had or something of the object must look at the side of the movement? can be adjustable?
Children (0):
Static Translation: (15.0503, 34.1802, -118.183)
The text you have provided is not csv.
For a complex structure as this one, you may need to go for JSON or any other hierarchical format.
Hi @Hellium it works super nice. Do you know how to rotate one side of the object into movement side?
What do you mean by "movement side"? Your new question is quite off-topic from the original one. You should post another question.
Hello @Hellium, thanks a lot for this solution. Im trying to adapt this code to move multiple gameobjects at the same time. I succeeded by writing lines for speed and destination for each objects. But, at the end, there is a lot of lines and its not a good thing. I don't know if its possible to use a loop foreach in the step structure, by i get some issues trying to do this. I probably need a class, but Im not sure how to use it. You probably understood that Im a beginner, so I hope you can help me with this issue. Thanks. Anthony
Why don't you create multiple CSV files, one per object to move?
The number of GameObject is variable and don't always have the same name. Its sports data and number of players and their id is variable. If I can choose a game among multiple game later, i prefer call two csv than 40. So for one game, i've got two csv with approximately 20 columns each separated by ','. In each player column, i've got 3 data separated by ';' (x, y, speed) I finally succeeded by created a list of array of string and i update the data using list[row number][column number], incremanting row number at each update and column number in a foreach player loop. I couldn't do this without your script at first basis. Thanks.
Hello! Thank you for your help! I have one more problem. Now the particle system will move back as a loop. How can I just let it moves to the last point I set forever?
Replace
if((Cube.position - steps[currentStep].destination).sqrMagnitude < 0.1f)
currentStep = (currentStep + 1) % steps.Count;
By
if((Cube.position - steps[currentStep].destination).sqrMagnitude < 0.1f && current step < steps.Count - 1)
++currentStep;
Thanks a lot! It works. Then I find out the "current step" inside your new code should be replaced by "currentStep". But everything else are correct. Appreciate!
Your answer
Follow this Question
Related Questions
cube stacking while parented 0 Answers
Need help with movement of UI elements position, in comparison to the canvas size. 2 Answers
Spawn Procedural Planet at parent's position 0 Answers
What is the best way to sync object positions over a network with the LLAPI? 0 Answers
Image (WebCamTexture) jumps on Android 0 Answers