Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by soaringoksel · Jan 30, 2020 at 09:59 AM · positioningcsvtxtreading

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





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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]))
                 }
             );
         }
     }
 }
Comment
Add comment · Show 27 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image soaringoksel · Jan 30, 2020 at 11:37 AM 0
Share

Thanks a lot, dear @Hellium ! It works!

avatar image soaringoksel · Feb 18, 2020 at 03:09 PM 0
Share

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?

avatar image soaringoksel soaringoksel · Feb 18, 2020 at 03:10 PM 0
Share
  Children (0):
         Static Translation: (15.0503, 34.1802, -118.183) 
avatar image Hellium soaringoksel · Feb 18, 2020 at 03:22 PM 0
Share

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.

Show more comments
avatar image soaringoksel · Mar 05, 2020 at 03:41 PM 0
Share

Hi @Hellium it works super nice. Do you know how to rotate one side of the object into movement side?

avatar image Hellium soaringoksel · Mar 05, 2020 at 03:53 PM 1
Share

What do you mean by "movement side"? Your new question is quite off-topic from the original one. You should post another question.

avatar image AnthonyBrossault · Dec 16, 2020 at 10:04 AM 0
Share

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

avatar image Hellium AnthonyBrossault · Dec 18, 2020 at 09:45 PM 0
Share

Why don't you create multiple CSV files, one per object to move?

avatar image AnthonyBrossault Hellium · Dec 19, 2020 at 08:51 AM 0
Share

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.

avatar image luxiaoqi0830 · Jun 21, 2021 at 12:22 AM 0
Share

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?

avatar image Hellium luxiaoqi0830 · Jun 21, 2021 at 06:39 AM 1
Share

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;
avatar image luxiaoqi0830 Hellium · Jun 21, 2021 at 03:52 PM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

197 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate in wrong position 0 Answers

Unity2D trouble positioning object in script 1 Answer

How to properly position gameObjects one after the other (C#) 1 Answer

Rigidbody2D.Postion has not moved object even after many Fixed Updates. 0 Answers

I'd like to make an object follow the mouse along a plane when the camera is at an angle 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges